According to the traditional rules of WordPress sites, the user’s profile information is displayed on the post’s detail page. Typically, the post is displayed, and the profile of the author who wrote the post is shown. When someone writes a post on the WordPress dashboard, the user’s name can be selected and saved with the post.
If you want to display user profile information on your website, then this tutorial can help you. Usually, the user or other profile is shown at the bottom of the info post.
After opening the single.php file in your theme, copy and paste the code snippet below wherever you want the profile info to appear.
<?php global $current_user; get_currentuserinfo(); echo 'Username: ' . $current_user->user_login . "\n"; echo 'User email: ' . $current_user->user_email . "\n"; echo 'User first name: ' . $current_user->user_firstname . "\n"; echo 'User last name: ' . $current_user->user_lastname . "\n"; echo 'User display name: ' . $current_user->display_name . "\n"; echo 'User ID: ' . $current_user->ID . "\n"; ?>
If you use the print_r command to inspect the WordPress global variable, $current_user, and what data is stored, we will see the following output. You should also type this code into a single.php file.
It turns out, $current_user returns an object. Using any of its “key“, I get the corresponding “value“.
WP_User Object ( [data] => stdClass Object ( [ID] => 1 [user_login] => admin [user_pass] => $P$Bs8JMpVBy0pBR.bt1X4Jlv4.WTOlGi. [user_nicename] => admin [user_email] => firstname.lastname@example.org [user_url] => http://website.com [user_registered] => 2019-12-11 07:03:19 [user_activation_key] => [user_status] => 0 [display_name] => Mr. User ) [ID] => 1 [caps] => Array ( [administrator] => 1 ) [cap_key] => hm_capabilities [roles] => Array (  => administrator ) [allcaps] => Array ( [switch_themes] => 1 [edit_themes] => 1 [activate_plugins] => 1 [edit_plugins] => 1 [edit_users] => 1 [edit_files] => 1 [manage_options] => 1 [moderate_comments] => 1 [manage_categories] => 1 [manage_links] => 1 [upload_files] => 1 [import] => 1 [unfiltered_html] => 1 [edit_posts] => 1 [edit_others_posts] => 1 [edit_published_posts] => 1 [publish_posts] => 1 [edit_pages] => 1 [read] => 1 [level_10] => 1 [level_9] => 1 [level_8] => 1 [level_7] => 1 [level_6] => 1 [level_5] => 1 [level_4] => 1 [level_3] => 1 [level_2] => 1 [level_1] => 1 [level_0] => 1 [edit_others_pages] => 1 [edit_published_pages] => 1 [publish_pages] => 1 [delete_pages] => 1 [delete_others_pages] => 1 [delete_published_pages] => 1 [delete_posts] => 1 [delete_others_posts] => 1 [delete_published_posts] => 1 [delete_private_posts] => 1 [edit_private_posts] => 1 [read_private_posts] => 1 [delete_private_pages] => 1 [edit_private_pages] => 1 [read_private_pages] => 1 [delete_users] => 1 [create_users] => 1 [unfiltered_upload] => 1 [edit_dashboard] => 1 [update_plugins] => 1 [delete_plugins] => 1 [install_plugins] => 1 [update_themes] => 1 [install_themes] => 1 [update_core] => 1 [list_users] => 1 [remove_users] => 1 [promote_users] => 1 [edit_theme_options] => 1 [delete_themes] => 1 [export] => 1 [administrator] => 1 ) [filter] => [site_id:WP_User:private] => 1 )
In case, if you have installed the Yoast SEO plugin and want to display users’ social profile info, such as Twitter or Facebook, take the following lines of code in your single.php file.
<?php global $current_user; get_currentuserinfo(); // get the current users ID $user_id = $current_user->ID; // Get Twitter meta data $user_twitter = get_user_meta( $user_id, 'twitter', true); echo $user_twitter; ?>
In order to display user profile information correctly, all user information (eg Username, First Name, Last Name, Nickname, Display name, Email, Website, Biographical Info) should be saved in the WordPress dashboard.
It is important to remember that if a photograph is uploaded to a user’s Gravatar account, it is possible to show the user’s picture in the user profile; For this, use the following code:
<?php echo get_avatar( get_the_author_meta( 'ID' ), 160 ); ?>
If you liked this how-to post, please consider checking out our other articles on the site like Speed up WordPress Website: Tips to Reduce Load Time, or, Move WordPress Site from Localhost to Live Server: Step-by-step Tutorial with Screenshots.
Leave Your Comment