wppaste
WordPress

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.

Retrieves user info by user 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?

It delegates straight to get_user_by('id', $user_id), which returns false whenever no row matches that ID, so any invalid, deleted, or non-existent ID produces false rather than an error. - Always check the result with 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?

No. The function only forwards to get_user_by('id', ...), so it is hard-wired to the ID field and ignores logins, emails, and slugs entirely. - Use 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?

The function has no idea who is logged in; it simply looks up whatever numeric $user_id you pass it and does not read the current-user global. - Get the logged-in user's ID first with 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

Reaches the database via get_user_by().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
6

Executed per call on PHP 8.5. The body compiles to 6.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
50

50 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • querycontent queryget_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.

WhenInstructionsCalls it makes
always6get_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

Used by · 50

Show all 50

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 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.