0

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.

  • Do you have the user ID of the user you'd like to display these details for? – Tom J Nowell Nov 8 '15 at 5:34
  • Yes, I have the user ID and the email – Justin Munce Nov 8 '15 at 6:44
  • 1
    It looks like there's some HTML code missing in your snippet – birgire Nov 8 '15 at 11:15
3

You can make use of

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

  • 1
    @birgire thanks for the edit. The hand was faster than the brain there. – Pieter Goosen Nov 8 '15 at 11:29
  • Thanks for the code on current_user. Actually, I have that info too, from the codex. I'm trying to display a specified user's info though. For example, on Page1, display user Walt Whitman's picture, description (bio), and social media links, regardless of who wrote the page or who is logged in reading the page. – Justin Munce Nov 8 '15 at 22:59
  • @JustinMunce Then simply feed the user ID of Walt Whiteman to get_user_meta(). I do not understand what your issue is. – Pieter Goosen Nov 9 '15 at 3:53
  • Hi Pieter, I apparently am unable to do that. Just spent another hour experimenting with _meta and _description code and haven't been able to make the bio display on the page, unfortunately – Justin Munce Nov 9 '15 at 7:49
  • But I'm not trying to display the logged-in user's information. I'm trying to display the information for a specific user of the site "Walt Whitman's bio" – Justin Munce Nov 9 '15 at 8:02
1

You can use get_user_by to get their User ID from the email address since it looks like that's the data you have available above. You can then use the get_userdata or get_user_meta from that user ID to pull all of their other meta fields.

For example:

$user = get_user_by( 'email', 'useremail@gmail.com' );
$userdata = get_userdata( $user->ID );

$first_name = $userdata-> first_name;
  • Ok, but how can I use those commands to display the particular (bio) meta field for a given user ID? – Justin Munce Nov 9 '15 at 7:49
  • get_usermeta return a whole wealth of info about the user including description. So, in the example above we would reference $userdata->description to get the bio field. To call the userdata function to begin with just skip my first line and feed the ID into the get_userdata function. – Mike Selander Nov 10 '15 at 4:21
  • Hi Mike, I understand get_usermeta and I am able to use it to display all a user's information. What I am not able to do is call up the user and display the bio / social media buttons. I don't know how to code it, in other words. – Justin Munce Nov 10 '15 at 4:50
  • if when you dont have the field value but have the url for example if the slug https://myexample.com/user/joe/ how to get value the slug – Firefog Jul 1 '18 at 10:50
0

(I am the op.)

Two options to solve this. These can be used to display any information for the users registered on your website. I'm going to show you how to display one OR two (ie more) things:

1) Pieter Goosen way (I checked his answer as correct above -- thanks again, Pieter) (People, you need to go into the user profile and get their user number. Use that number instead of 9999.

One thing:

<?php $walt_id = 9999; // Make sure you have the correct ID here
$userdata = get_user_meta( $walt_id );
echo $userdata['description'][0]; 
?>

More than one thing:

<?php $walt_id = 9999; // Make sure you have the correct ID here
$userdata = get_user_meta( $walt_id );
echo $userdata['description'][0]; 
echo $userdata['first_name'][0]; 
?>

2) stephencottontail way:

One thing:

<?php the_author_meta( 'user_description', 9999 ); ?> </br>

More than one thing:

<?php the_author_meta( 'user_description', 9999 ); ?> </br>
<?php the_author_meta( 'first_name', 9999 ); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.