WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/get userdata

Description

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').

Usage

 <?php get_userdata$userid ); ?> 

Parameters

$userid
(integer) (required) The ID of the user whose data should be retrieved.
Default: None

Return Values

(bool|object) 
False on failure, WP_User object on success.

Examples

Basic Usage

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:
Username: admin
User roles: administrator
User ID: 1

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:
Hi, your login name: admin@example.com, your email: admin@example.com, your author url: example.com/author/adminexample-com

Note: user_nicename is the URL sanitized version of user_login :)

Accessing Usermeta Data

<?php $user_info = get_userdata(1);
      echo $user_info->last_name .  ", " . $user_info->first_name . "\n";
?>
Results in:
Doe, John

Notes

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:

  • users
    • ID
    • user_login
    • user_pass
    • user_nicename
    • user_email
    • user_url
    • user_registered
    • display_name
  • user_meta
    • first_name
    • last_name
    • nickname
    • description
    • wp_capabilities (array)
    • admin_color (Theme of your admin page. Default is fresh.)
    • closedpostboxes_page
    • primary_blog
    • rich_editing
    • source_domain

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).

Source File

get_userdata() is located in wp-includes/pluggable.php.

See Also

Related

Get User Functions:

See also index of Function Reference and index of Template Tags.