Function Reference/get userdata

跳转至: 导航搜索


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


%%% <?php get_userdata( $userid ); ?> %%%


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

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9


This template is for standardizing how parameters look in the Function Reference and in Template Tags. Here is an example of this template being called: prenowiki检查到模板循环:模板:Parameter/nowiki/pre 检查到模板循环:模板:Parameter

The usage of this template is below: prenowiki检查到模板循环:模板:Parameter/nowiki/pre Let's take a closer look at the parameters..

The name of the parameter.
The datatype that should be given for this parameter when called.
  • string
  • integer
  • boolean
  • mixed
A short description of the parameter.
Set this parameter to optional if the parameter is optional. Otherwise, do not declare this parameter—it defaults to required.
  • required
  • optional
If this parameter is optional, ttdefault/tt is the value that will be used if the parameter is not declared. /div /noinclude

Return Values

False on failure, WP_User object on success.


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


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

Source File

get_userdata() is located in onlyincludecodewp-includes/pluggable.php/code/onlyinclude

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Template Description

Link to the source code on


  1. filename
  2. (option) path to codetag/code (version) or codetrunk/code. This option is only used for a new /Default: codetrunk/code -- trunk is the latest bleeding edge development version of WordPress.


Link to the stable version: pre检查到模板循环:模板:Trac/pre

Link to trunk: pre检查到模板循环:模板:Trac/pre


See Also


includeonlydiv style=clear:both; background-color:#F7F7F7; border:1px solid #CCCCCC; color:#000000; padding:7px; margin:0.5em auto 0.5em auto; vertical-align:middle;See also index of Function Reference and index of Template Tags./div/includeonlynoinclude


This Template is used by Codex:Template Messages.


pre 检查到模板循环:模板:Message /pre