Codex

Function Reference/get userdata

Contents

Description

Returns a WP_User object with the information pertaining to the user whose ID is passed to it. Properties 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

Default Usage

get_userdata() returns an object of the user's data that you can assign to a variable. After that, 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 level: ' . $user_info->user_level . "\n";
      echo 'User ID: ' . $user_info->ID . "\n";
?>

Results in:

Username: admin
User level: 10
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);
      $username = $user_info->user_login;
      $first_name = $user_info-> user_firstname;
      $last_name = $user_info-> user_lastname;
      echo "$first_name $last_name logs into her WordPress site with the user name of $username.";
?>

Results in:

Harriet Smith logs into her WordPress site with the user name of mrssmith.

Accessing Usermeta Data

<?php $user_info = get_userdata(1);
      echo $user_info-> user_lastname .  ", " . $user_info-> user_firstname . "\n";
?>

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
    • user_firstname
    • user_lastname
    • nickname
    • user_description
    • wp_capabilities (array)
    • admin_color (Theme of your admin page. Default is fresh.)
    • closedpostboxes_page
    • primary_blog
    • rich_editing
    • source_domain

Note that after wp 3.2, the return value has changed, a WP_User object is returned instead of a dumb object. However, through the usage of magic methods, data fields (such as the meta key rich_editing), get_userdata(1)->rich_editing will map to get_userdata(1)->data->rich_editing, even though in a var_dump() or similar will not show these extra fields. Refer http://core.trac.wordpress.org/ticket/18551.

Source File

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

See Also

Related

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