I’d like to (in php) display a given user’s avatar and bio (description) and some social media for them (based on their profile info fields). For example, on Page1, display user Walt Whitman’s (user number 9999) picture, description (bio), and social media links, regardless of who wrote the page or who is logged in reading the page.

I can display the avatar:

<?php echo get_avatar( 'useremail@gmail.cam', 32 ); ?>

I’ve tried a couple of things, but they don’t work. I get either a full display of all meta or a fatal error (various fatal errors like ‘must be string’ etc).

<?php the_user_meta( 'description' ); ?>

or

<?php $userdata = get_user_meta( 9999 ); ?><?php echo $userdata['description']; ?> 

If I use

<?php $user = wp_get_current_user( 9999 ); if ( $user->exists() ) // is_user_logged_in() is a wrapper for this line $userdata = get_user_meta( $user->data->ID ); ?><pre><?php var_dump( $userdata ); ?></pre><?php echo $userdata['description'] ; ?>

I get a fatal error.

3 Answers
3

You can make use of

  • wp_get_current_user() or

  • get_currentuserinfo() (to which wp_get_current_user() is a wrapper function to) or

  • get_current_user_id() which uses wp_get_current_user()

to get the current logged in user ID.

One way or the other, you need to make sure that you have a logged in user (user ID is not 0) before trying to get the user’s metadata from the db.

Once you have the user ID, you can use get_user_meta() to return the user’s info from the db

EXAMPLE:

$user = wp_get_current_user();
if ( $user->exists() ) { // is_user_logged_in() is a wrapper for this line
    $userdata = get_user_meta( $user->data->ID );
    ?><pre><?php var_dump( $userdata ); ?></pre><?php
}

EDIT

This is very basic PHP. Here is what is returned by

$userdata = get_user_meta( 1 );
    ?><pre><?php var_dump( $userdata ); ?></pre><?php

The var_dump()

array(29) {
  ["first_name"]=>
  array(1) {
    [0]=>
    string(6) "Pieter"
  }
  ["last_name"]=>
  array(1) {
    [0]=>
    string(6) "Goosen"
  }
  ["nickname"]=>
  array(1) {
    [0]=>
    string(12) "pietergoosen"
  }
  ["description"]=>
  array(1) {
    [0]=>
    string(349) "My naam is Pieter Goosen BLAH BLAH BLAH"
  }
  ["rich_editing"]=>
  array(1) {
    [0]=>
    string(4) "true"
  }
  ["comment_shortcuts"]=>
  array(1) {
    [0]=>
    string(5) "false"
  }
  ["admin_color"]=>
  array(1) {
    [0]=>
    string(5) "fresh"
  }
  ["use_ssl"]=>
  array(1) {
    [0]=>
    string(1) "0"
  }
  ["show_admin_bar_front"]=>
  array(1) {
    [0]=>
    string(5) "false"
  }
  ["wp_capabilities"]=>
  array(1) {
    [0]=>
    string(31) "a:1:{s:13:"administrator";b:1;}"
  }
  ["wp_user_level"]=>
  array(1) {
    [0]=>
    string(2) "10"
  }
  ["dismissed_wp_pointers"]=>
  array(1) {
    [0]=>
    string(143) "wp330_toolbar,wp330_saving_widgets,wp340_choose_image_from_library,wp340_customize_current_theme_link,wp350_media,wp360_revisions,wp390_widgets"
  }
  ["show_welcome_panel"]=>
  array(1) {
    [0]=>
    string(1) "0"
  }
  ["wp_user-settings"]=>
  array(1) {
    [0]=>
    string(120) "libraryContent=browse&imgsize=full&align=right&editor=html&hidetb=1&mfold=o&unfold=1&urlbutton=post&posts_list_mode=list"
  }
  ["wp_user-settings-time"]=>
  array(1) {
    [0]=>
    string(10) "1447257892"
  }
  ["wp_dashboard_quick_press_last_post_id"]=>
  array(1) {
    [0]=>
    string(3) "463"
  }
  ["twitter"]=>
  array(1) {
    [0]=>
    string(0) ""
  }
  ["facebook"]=>
  array(1) {
    [0]=>
    string(15) "pietergoosencom"
  }
  ["managenav-menuscolumnshidden"]=>
  array(1) {
    [0]=>
    string(89) "a:4:{i:0;s:11:"link-target";i:1;s:11:"css-classes";i:2;s:3:"xfn";i:3;s:11:"description";}"
  }
  ["metaboxhidden_nav-menus"]=>
  array(1) {
    [0]=>
    string(102) "a:4:{i:0;s:8:"add-post";i:1;s:14:"add-informasie";i:2;s:12:"add-post_tag";i:3;s:15:"add-post_format";}"
  }
  ["nav_menu_recently_edited"]=>
  array(1) {
    [0]=>
    string(3) "130"
  }
  ["closedpostboxes_page"]=>
  array(1) {
    [0]=>
    string(6) "a:0:{}"
  }
  ["metaboxhidden_page"]=>
  array(1) {
    [0]=>
    string(94) "a:4:{i:0;s:10:"postcustom";i:1;s:16:"commentstatusdiv";i:2;s:7:"slugdiv";i:3;s:9:"authordiv";}"
  }
  ["closedpostboxes_post"]=>
  array(1) {
    [0]=>
    string(6) "a:0:{}"
  }
  ["metaboxhidden_post"]=>
  array(1) {
    [0]=>
    string(6) "a:0:{}"
  }
  ["closedpostboxes_positions"]=>
  array(1) {
    [0]=>
    string(6) "a:0:{}"
  }
  ["metaboxhidden_positions"]=>
  array(1) {
    [0]=>
    string(6) "a:0:{}"
  }
  ["rtladminbar"]=>
  array(1) {
    [0]=>
    string(3) "ltr"
  }
  ["session_tokens"]=>
  array(1) {
    [0]=>
    string(285) "a:1:{s:64:"fa12574e7a42af2a8944d764c21bda64a5a5ee4572b1fbceb027d8b4af5afcd3";a:4:{s:10:"expiration";i:1448467488;s:2:"ip";s:3:"::1";s:2:"ua";s:108:"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36";s:5:"login";i:1447257888;}}"
  }
}

So, if you need to display the description, you can do

echo $userdata['description'][0];

To learn how to reference values in an array, you should really need and go and learn the very basics of how arrays work and how to reference them

EDIT

The following is an exact use case

$walt_id = 1; // Make sure you have the correct ID here
$userdata = get_user_meta( $walt_id );
echo $userdata['description'][0];

If this does not work, you have a serious issue somewhere which you should debug as I have stated in comments

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *