get_userdata( int $user_id ): WP_User|false
- Since
- 0.71
- Source
wp-includes/pluggable.php:83
Looks up a single user's data by their numeric ID and returns a WP_User object, or false if no user exists with that ID. It is a thin wrapper around get_user_by('id', $user_id), so it only accepts an ID, never a login, email, or slug. Reach for get_user_by() directly if you need to look someone up by something other than their ID.
Compatibility
- WordPress
- since 0.71
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$user_idint- User ID
Return value
WP_User|false- WP_User object on success, false on failure.
Code examples
Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.
Show the display name and email of the currently logged-in user
The sandbox has an administrator logged in, so get_current_user_id() returns a real ID to look up.
$user_id = get_current_user_id();
$user = get_userdata( $user_id );
if ( $user instanceof WP_User ) {
echo esc_html( 'Logged in as ' . $user->display_name . ' <' . $user->user_email . '>' );
} else {
echo esc_html( 'No user found for ID ' . $user_id );
}Look up the author of a post and print their role
Post 2 from the baseline fixtures has an author ID stored on the post row, which get_userdata() can resolve to a full user object.
$post_id = 2;
$author_id = (int) get_post_field( 'post_author', $post_id );
$author = get_userdata( $author_id );
if ( $author instanceof WP_User ) {
printf(
'Post %d was written by %s (role: %s)',
absint( $post_id ),
esc_html( $author->display_name ),
esc_html( implode( ', ', $author->roles ) )
);
} else {
echo esc_html( 'Post ' . $post_id . ' has no resolvable author.' );
}Common problems and fixes · 3
- Why does get_userdata() return false instead of a user object?
- Can I use get_userdata() with a username or email address instead of an ID?
- Why doesn't get_userdata() automatically return the logged-in user?
Why does get_userdata() return false instead of a user object?
instanceof WP_User or === false before reading properties like $user->display_name.
- Double-check the ID is actually numeric and not accidentally a login or email string.Can I use get_userdata() with a username or email address instead of an ID?
get_user_by( 'login', $username ) for a username.
- Use get_user_by( 'email', $email ) for an email address.Why doesn't get_userdata() automatically return the logged-in user?
get_current_user_id(), then pass that ID in.
- Or skip the lookup entirely and call wp_get_current_user() directly.Alternatives and related functions
get_user_by- When you need to look a user up by login, email, or slug instead of a numeric ID.
wp_get_current_user- When you want the currently logged-in user's data without first fetching their ID.
get_users- When you need a list of users matching certain criteria rather than a single known ID.
Performance profile
How much work a call to get_userdata() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Constant
- Instructions
- 6
- Plugin surface
- None
- Called by
- 50
Reaches the database via get_user_by().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5. The body compiles to 6.
Nothing here hands control to plugin code.
50 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_user_by()called directly
What one call costs · 1 distinct outcome
One number would be a lie: the work depends on which branch runs. These are every distinct cost get_userdata() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 6 | get_user_by() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 6 instructions, 6 executed per call, 0 branches. The work does not change between versions.
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 1
- get_user_by()Retrieves user info by a given field.
Used by · 50
- WP::register_globals()Set up the WordPress Globals.
- WP_Customize_Manager::get_lock_user_data()Gets lock user data.
- WP_Posts_List_Table::column_title()Handles the title column output.
- WP_Query::generate_postdata()Generates post data.
- WP_Query::get_queried_object()Retrieves the currently queried object.
- WP_REST_Application_Passwords_Controller::get_user()Gets the requested user.
- WP_REST_Posts_Controller::prepare_item_for_database()Prepares a single post for create or update.
- WP_REST_Templates_Controller::prepare_item_for_database()Prepares a single template for create or update.
- WP_REST_Users_Controller::delete_item()Deletes a single user.
- WP_REST_Users_Controller::get_user()Get the user, if the ID is valid.
- WP_Users_List_Table::single_row()Generates HTML for a single row on the users.php admin panel.
- _admin_notice_post_locked()Outputs the HTML for the notice to say that someone else is editing or has taken over editing of this post.
Show all 50
- add_user_to_blog()Adds a user to a blog, along with specifying the user's role.
- author_can()Returns whether the author of the supplied post has the specified capability.
- confirm_delete_users()
- default_password_nag_edit_user()
- edit_user()Edit user settings based on contents of $_POST
- get_author_posts_url()Retrieves the URL to the author page for the user with the ID provided.
- get_comment_author()Retrieves the author of the current comment.
- get_comment_class()Returns the classes for the comment div as an array.
- get_edit_user_link()Retrieves the edit user link.
- get_editable_user_ids()Gets the IDs of any users who can edit posts.
- get_oembed_response_data()Retrieves the oEmbed response data for a given post.
- get_permalink()Retrieves the full permalink for the current post or post ID.
- get_the_author_meta()Retrieves the requested data of the author of the current post.
- get_the_modified_author()Retrieves the author who last edited the current post.
- get_user_option()Retrieves user option that can be either per Site or per Network.
- get_user_to_edit()Retrieve user data and filter it.
- grant_super_admin()Grants Super Admin privileges.
- is_super_admin()Determines whether user is a site admin.
- is_user_member_of_blog()Finds out whether a user is a member of a given blog.
- newuser_notify_siteadmin()Notifies the network admin that a new user has been activated.
- redirect_canonical()Redirects incoming links to the proper URL based on the site url.
- refresh_user_details()Cleans the user cache for a specific user.
- remove_user_from_blog()Removes a user from a blog.
- revoke_super_admin()Revokes Super Admin privileges.
- setup_userdata()Sets up global user vars.
- twentynineteen_is_comment_by_post_author()Returns true if comment is by author of the post.
- twentytwenty_is_comment_by_post_author()Checks if the specified comment is written by the author of the post commented on.
- user_can()Returns whether a particular user has the specified capability.
- user_can_create_draft()Whether user can create a post.
- user_can_create_post()Whether user can create a post.
- user_can_edit_post()Whether user can edit a post.
- user_can_edit_post_date()Whether user can delete a post.
- user_can_edit_user()Can user can edit other user.
- user_can_for_site()Returns whether a particular user has the specified capability for a given site.
- user_can_set_post_date()Whether user can set new posts' dates.
- wp_admin_bar_edit_menu()Provides an edit link for posts and terms.
- wp_ajax_add_user()Handles adding a user via AJAX.
- wp_ajax_destroy_sessions()Handles destroying multiple open sessions for a user via AJAX.
Source code
function get_userdata( $user_id ) { return get_user_by( 'id', $user_id ); }Changelog
Introduced in 0.71. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 tag, from
src/wp-includes/pluggable.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.