目录
描述
译文
返回的对象中含有用户相关信息,用户编号被传递。函数属性直接映射到数据库的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').
用法
<?php get_userdata( $userid ); ?>
参数
$userid
(integer) (必填) The ID of the user whose data should be retrieved.
默认值: None
返回值
示例
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:
1 2 3 4 5 6 7 8 9 10 11 |
/* ---------------------------------- * 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:
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:
1 2 3 4 5 6 7 8 9 |
/* ---------------------------------- * 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:
Accessing Usermeta Data
1 2 3 4 5 6 7 |
/* ---------------------------------- * wordpress之魂 © http://wphun.com * ---------------------------------- */ <?php $user_info = get_userdata(1); echo $user_info->last_name . ", " . $user_info->first_name . " "; ?> |
Results in:
注意
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() 函数的代码位于 wp-includes/pluggable.php
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
/* ---------------------------------- * 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() 源文件
- 原文:http://codex.wordpress.org/Function_Reference/get_userdata
- 翻译:黄聪@WordPress之魂