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( '[email protected]', 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.
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( '[email protected]', 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.
Share Improve this question edited Nov 12, 2015 at 4:12 Justin Munce asked Nov 8, 2015 at 5:32 Justin MunceJustin Munce 1991 gold badge1 silver badge9 bronze badges 3- Do you have the user ID of the user you'd like to display these details for? – Tom J Nowell ♦ Commented Nov 8, 2015 at 5:34
- Yes, I have the user ID and the email – Justin Munce Commented Nov 8, 2015 at 6:44
- 1 It looks like there's some HTML code missing in your snippet – birgire Commented Nov 8, 2015 at 11:15
3 Answers
Reset to default 7You can make use of
wp_get_current_user()
orget_currentuserinfo()
(to whichwp_get_current_user()
is a wrapper function to) orget_current_user_id()
which useswp_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
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', '[email protected]' );
$userdata = get_userdata( $user->ID );
$first_name = $userdata-> first_name;
(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 ); ?>