Function Reference/get userdata

来自WordPress中文文档
跳转至: 导航搜索

wordpress.org.cn

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

tt$userid/tt
(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

Notes

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

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

wordpress.org.cn /div /noinclude

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 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 http://core.trac.wordpress.org/browser/.

Parameters

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

Usage

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

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

/div

wordpress.org.cn.

See Also

Related

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

Description

This Template is used by Codex:Template Messages.

Usage

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

Result

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

/noinclude