get_userdata()获取某个用户的所有信息

目录

描述

译文

返回的对象中含有用户相关信息,用户编号被传递。函数属性直接映射到数据库的wp_users表中(参见Database 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').

get_userdata() 描述

用法

 <?php get_userdata$userid ); ?> 

get_userdata() 用法

参数

$userid

(integer) (必填) The ID of the user whose data should be retrieved.

默认值: None

get_userdata() 参数

返回值

(bool|object) 

False on failure, WP_User object on success.

get_userdata() 返回值

示例

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:

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?php $user_info = get_userdata(1);
      echo 'Username: ' . $user_info->user_login . "
";
      echo 'User roles: ' . implode(', ', $user_info->roles) . "
";
      echo 'User ID: ' . $user_info->ID . "
";
?>

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:

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?php $user_info = get_userdata(1);
      $username = $user_info->user_login;
      $first_name = $user_info->first_name;
      $last_name = $user_info->last_name;
      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

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?php $user_info = get_userdata(1);
      echo $user_info->last_name .  ", " . $user_info->first_name . "
";
?>

Results in:

Doe, John

get_userdata() 示例

注意

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

get_userdata() 注意

源文件

get_userdata() 函数的代码位于 wp-includes/pluggable.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Retrieve user info by user ID.
 *
 * @since 0.71
 *
 * @param int $user_id User ID
 * @return WP_User|false WP_User object on success, false on failure.
 */
function get_userdata( $user_id ) {
	return get_user_by( 'id', $user_id );
}
endif;

if ( !function_exists('get_user_by') ) :

get_userdata() 源文件

相关