Languages: English • 한국어 • Türkçe • 日本語 (Add your language)
Returns a WP_User object with the information pertaining to the user whose ID is passed to it. Properties de-referenced with "->" map directly to wp_users and wp_usermeta tables in the database (see Database Description).
If the user does not exist, the function returns false.
An alias of get_user_by('id').
<?php get_userdata( $userid ); ?>
The get_userdata() function returns an object of the user's data. You can echo various parts of the returned object or loop through the data to display it all.
Example displaying certain parts:
<?php $user_info = get_userdata(1);
echo 'Username: ' . $user_info->user_login . "\n";
echo 'User roles: ' . implode(', ', $user_info->roles) . "\n";
echo 'User ID: ' . $user_info->ID . "\n";
?>
Results in: You can also assign certain parts into individual variables for displaying later or in multiple places.
Example for extracting certain parts:
<?php $user_info = get_userdata(1);
$userloginname = $user_info->user_login;
$nicename = $user_info->user_nicename;
$email = $user_info->user_email;
echo "Hi, your login name: {$userloginname}, your email: {$email}, your author url: example.com/author/{$nicename} ";
?>
Results in: Note: user_nicename is the URL sanitized version of user_login :)
<?php $user_info = get_userdata(1);
echo $user_info->last_name . ", " . $user_info->first_name . "\n";
?>
Results in: Here are some of the useful values in the wp_users and wp_usermeta tables you can access with this function for use in your theme or plugin:
Note: the WP_User object uses PHP 5 "magic" methods to provide some of its properties. For example:
$user_info->user_login is shorthand for $user_info->data->user_login, and
$user_info->rich_editing is shorthand for get_user_meta($user_info->ID, 'rich_editing', true).
get_userdata() is located in wp-includes/pluggable.php.
Get User Functions: