WordPress pluggins – How to get user data in your pluggin

So I’ve been working on a WordPress Pluggin for use on a site and found that while there is heaps of usefull snippets of code that help, especially on stackoverflow.com it can sometimes take some googling to find it! Here is a useful bit of information for retrieving user data in your pluggin.

using the get_userdata to retrieve information on a particular user.

$user_info = get_userdata(1); // 1 is user ID here (required)
echo "
Username: " . $user_info->user_login . " <br />
User level: " . $user_info->user_level . " <br />
User ID: " . $user_info->ID . "<br />
Display Name: " . $user_info->display_name . " ";

Getting the details of the Currently logged in user with get_currentuserinfo

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";

Reference: Stackoverflow

Share

Tags: , , , , ,

No comments yet.

Leave a Reply