get_userdata()
概述
get_userdata() 函数通过用户 ID 检索用户信息,返回一个 WP_User 对象或失败时返回 false。它是基于 get_user_by() 的封装函数,常用于获取用户数据以进行显示或权限检查。
关键要点
- 参数:$user_id(整数,必需),指定要检索的用户 ID。
- 返回值:成功时返回 WP_User 对象,失败时返回 false。
- 内部实现:调用 get_user_by('id', $user_id) 来获取用户数据。
- 用途广泛:被多个核心函数和类使用,涉及用户管理、权限验证、REST API、XML-RPC 等场景。
代码示例
$user_info = get_userdata(1);
echo 'Username: ' . $user_info->user_login . "n";
echo 'User roles: ' . implode(', ', $user_info->roles) . "n";
echo 'User ID: ' . $user_info->ID . "n";注意事项
- 确保传入有效的用户 ID,否则可能返回 false。
- 返回的 WP_User 对象包含用户的基本信息和元数据,可通过对象属性访问(如 user_login、roles、first_name 等)。
- 在性能敏感场景中,考虑直接使用 get_user_by() 或其他缓存机制。
Retrieves user info by user ID.
Parameters
$user_idintrequired-
User ID
Return
Source
function get_userdata( $user_id ) {
return get_user_by( 'id', $user_id );
}
Related
| Uses | Description |
|---|---|
get_user_by()wp-includes/pluggable.php |
Retrieves user info by a given field. |
| Used by | Description |
|---|---|
user_can_for_site()wp-includes/capabilities.php |
Returns whether a particular user has the specified capability for a given site. |
wp_check_comment_data()wp-includes/comment.php |
Checks whether comment data passes internal checks or has disallowed content. |
wp_is_password_reset_allowed_for_user()wp-includes/user.php |
Checks if password reset is allowed for a specific user. |
wp_list_users()wp-includes/user.php |
Lists all the users of the site, with several options available. |
WP_REST_Templates_Controller::prepare_item_for_database()wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php |
Prepares a single template for create or update. |
wp_ajax_send_password_reset()wp-admin/includes/ajax-actions.php |
Handles sending a password reset link via AJAX. |
wpmu_new_site_admin_notification()wp-includes/ms-functions.php |
Notifies the Multisite network administrator that a new site was created. |
WP_REST_Application_Passwords_Controller::get_user()wp-includes/rest-api/endpoints/class-wp-rest-application-passwords-controller.php |
Gets the requested user. |
wp_is_application_passwords_available_for_user()wp-includes/user.php |
Checks if Application Passwords is available for a specific user. |
WP_Query::generate_postdata()wp-includes/class-wp-query.php |
Generates post data. |
WP_Customize_Manager::get_lock_user_data()wp-includes/class-wp-customize-manager.php |
Gets lock user data. |
WP_REST_Users_Controller::get_user()wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php |
Get the user, if the ID is valid. |
WP_REST_Users_Controller::delete_item()wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php |
Deletes a single user. |
WP_REST_Posts_Controller::prepare_item_for_database()wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php |
Prepares a single post for create or update. |
get_oembed_response_data()wp-includes/embed.php |
Retrieves the oEmbed response data for a given post. |
WP_Posts_List_Table::column_title()wp-admin/includes/class-wp-posts-list-table.php |
Handles the title column output. |
wp_ajax_destroy_sessions()wp-admin/includes/ajax-actions.php |
Handles destroying multiple open sessions for a user via AJAX. |
wxr_authors_list()wp-admin/includes/export.php |
Outputs list of authors with posts. |
get_editable_user_ids()wp-admin/includes/deprecated.php |
Gets the IDs of any users who can edit posts. |
grant_super_admin()wp-includes/capabilities.php |
Grants Super Admin privileges. |
revoke_super_admin()wp-includes/capabilities.php |
Revokes Super Admin privileges. |
refresh_user_details()wp-admin/includes/ms.php |
Cleans the user cache for a specific user. |
wp_check_locked_posts()wp-admin/includes/misc.php |
Checks lock status for posts displayed on the Posts screen. |
wp_refresh_post_lock()wp-admin/includes/misc.php |
Checks lock status on the New/Edit Post screen and refresh the lock. |
default_password_nag_edit_user()wp-admin/includes/user.php |
|
edit_user()wp-admin/includes/user.php |
Edit user settings based on contents of $_POST |
get_user_to_edit()wp-admin/includes/user.php |
Retrieve user data and filter it. |
WP_Users_List_Table::single_row()wp-admin/includes/class-wp-users-list-table.php |
Generates HTML for a single row on the users.php admin panel. |
wp_check_post_lock()wp-admin/includes/post.php |
Determines whether the post is currently being edited by another user. |
_admin_notice_post_locked()wp-admin/includes/post.php |
Outputs the HTML for the notice to say that someone else is editing or has taken over editing of this post. |
wp_ajax_wp_fullscreen_save_post()wp-admin/includes/ajax-actions.php |
Handles saving posts from the fullscreen editor via AJAX. |
wp_ajax_add_user()wp-admin/includes/ajax-actions.php |
Handles adding a user via AJAX. |
wp_ajax_inline_save()wp-admin/includes/ajax-actions.php |
Handles Quick Edit saving a post from a list table via AJAX. |
confirm_delete_users()wp-admin/includes/ms.php |
|
author_can()wp-includes/capabilities.php |
Returns whether the author of the supplied post has the specified capability. |
user_can()wp-includes/capabilities.php |
Returns whether a particular user has the specified capability. |
is_super_admin()wp-includes/capabilities.php |
Determines whether user is a site admin. |
wp_set_password()wp-includes/pluggable.php |
Updates the user’s password with a new hashed one. |
wp_new_user_notification()wp-includes/pluggable.php |
Emails login credentials to a newly-registered user. |
wp_notify_postauthor()wp-includes/pluggable.php |
Notifies an author (and/or others) of a comment/trackback/pingback on a post. |
wp_notify_moderator()wp-includes/pluggable.php |
Notifies the moderator of the site about a new comment that is awaiting approval. |
wp_generate_auth_cookie()wp-includes/pluggable.php |
Generates authentication cookie contents. |
user_can_create_post()wp-includes/deprecated.php |
Whether user can create a post. |
user_can_create_draft()wp-includes/deprecated.php |
Whether user can create a post. |
user_can_edit_post()wp-includes/deprecated.php |
Whether user can edit a post. |
user_can_set_post_date()wp-includes/deprecated.php |
Whether user can set new posts’ dates. |
user_can_edit_post_date()wp-includes/deprecated.php |
Whether user can delete a post. |
user_can_edit_user()wp-includes/deprecated.php |
Can user can edit other user. |
WP::register_globals()wp-includes/class-wp.php |
Set up the WordPress Globals. |
WP_Query::get_queried_object()wp-includes/class-wp-query.php |
Retrieves the currently queried object. |
get_edit_user_link()wp-includes/link-template.php |
Retrieves the edit user link. |
get_permalink()wp-includes/link-template.php |
Retrieves the full permalink for the current post or post ID. |
wp_admin_bar_edit_menu()wp-includes/admin-bar.php |
Provides an edit link for posts and terms. |
wp_update_user()wp-includes/user.php |
Updates a user in the database. |
wp_insert_user()wp-includes/user.php |
Inserts a user into the database. |
is_user_member_of_blog()wp-includes/user.php |
Finds out whether a user is a member of a given blog. |
setup_userdata()wp-includes/user.php |
Sets up global user vars. |
wp_dropdown_users()wp-includes/user.php |
Creates dropdown HTML content of users. |
get_user_option()wp-includes/user.php |
Retrieves user option that can be either per Site or per Network. |
redirect_canonical()wp-includes/canonical.php |
Redirects incoming links to the proper URL based on the site url. |
wpmu_welcome_user_notification()wp-includes/ms-functions.php |
Notifies a user that their account activation has been successful. |
wpmu_log_new_registrations()wp-includes/ms-functions.php |
Logs the user email, IP, and registration date of a new site. |
newuser_notify_siteadmin()wp-includes/ms-functions.php |
Notifies the network admin that a new user has been activated. |
wpmu_welcome_notification()wp-includes/ms-functions.php |
Notifies the site administrator that their site activation was successful. |
add_user_to_blog()wp-includes/ms-functions.php |
Adds a user to a blog, along with specifying the user’s role. |
remove_user_from_blog()wp-includes/ms-functions.php |
Removes a user from a blog. |
get_the_author_meta()wp-includes/author-template.php |
Retrieves the requested data of the author of the current post. |
get_author_posts_url()wp-includes/author-template.php |
Retrieves the URL to the author page for the user with the ID provided. |
wp_list_authors()wp-includes/author-template.php |
Lists all the authors of the site, with several options available. |
get_the_modified_author()wp-includes/author-template.php |
Retrieves the author who last edited the current post. |
wp_xmlrpc_server::mw_getPost()wp-includes/class-wp-xmlrpc-server.php |
Retrieves a post. |
wp_xmlrpc_server::mw_getRecentPosts()wp-includes/class-wp-xmlrpc-server.php |
Retrieves list of recent posts. |
wp_xmlrpc_server::mw_newPost()wp-includes/class-wp-xmlrpc-server.php |
Creates a new post. |
wp_xmlrpc_server::wp_getUser()wp-includes/class-wp-xmlrpc-server.php |
Retrieves a user. |
wp_xmlrpc_server::wp_getProfile()wp-includes/class-wp-xmlrpc-server.php |
Retrieves information about the requesting user. |
wp_xmlrpc_server::_prepare_page()wp-includes/class-wp-xmlrpc-server.php |
Prepares page data for return in an XML-RPC object. |
wp_xmlrpc_server::_insert_post()wp-includes/class-wp-xmlrpc-server.php |
Helper method for wp_newPost() and wp_editPost(), containing shared logic. |
get_comment_class()wp-includes/comment-template.php |
Returns the classes for the comment div as an array. |
get_comment_author()wp-includes/comment-template.php |
Retrieves the author of the current comment. |
Changelog
| Version | Description |
|---|---|
| 0.71 | Introduced. |
Skip to note 4 content
Codex
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:
user_login . "n"; echo 'User roles: ' . implode(', ', $user_info->roles) . "n"; echo 'User ID: ' . $user_info->ID . "n"; ?>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:
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.
Skip to note 5 content
Rohit Sharma
$user_info = get_userdata(1); $user_name = $user_info->display_name; $user_email = $user_info->user_email; echo $user_name .", ". $user_email;Result : John, John@example.com
Skip to note 6 content
Codex
Accessing Usermeta Data
last_name . ", " . $user_info->first_name . "n"; ?>Results in:
Doe, John