wppaste
WordPress

wp_get_current_user(): WP_User

Since
2.0.3
Source
wp-includes/pluggable.php:69

Fetches the WP_User instance representing whoever is currently logged in, setting the global current user first if it hasn't been initialized yet. If nobody is logged in, it returns a WP_User with ID 0, which has no capabilities and fails the usual logged-in checks. Use is_user_logged_in() or check the ->exists() method on the returned object before trusting its data, and reach for get_current_user_id() instead if all you need is the numeric ID.

Retrieves the current user object.

Description

Will set the current user, if the current user is not set. The current user will be set to the logged-in person. If no user is logged-in, then it will set the current user to 0, which is invalid and won't have any permissions.

Compatibility

WordPress
since 2.0.3
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.

Return value

WP_User
Current WP_User instance.

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 logged-in user

Print a quick greeting using the current user's stored profile fields.

$current_user = wp_get_current_user();

if ( $current_user->exists() ) {
	echo esc_html( 'Logged in as: ' . $current_user->display_name . ' (' . $current_user->user_email . ')' );
} else {
	echo esc_html( 'No user is currently logged in.' );
}

Check whether the logged-in user authored a given post

Compare the current user's ID against a post's author to decide whether to show an edit link.

$current_user = wp_get_current_user();
$post         = get_post( 2 );

if ( $post && (int) $post->post_author === $current_user->ID ) {
	echo esc_html( 'You wrote "' . $post->post_title . '", you can edit it.' );
} else {
	echo esc_html( 'You did not author this post.' );
}

The comparison assumes post_author is stored as a numeric string, so casting to int avoids a loose-comparison mismatch.

Common problems and fixes · 3

Why does wp_get_current_user() give me a user object with ID 0?

That's what the function returns when no one is logged in. WP_User(0) is a real object but has no valid ID, so any capability check against it fails silently instead of throwing an error.

Why do I get a fatal error calling wp_get_current_user() from a must-use plugin or the top of a plugin file?

wp_get_current_user() delegates to _wp_get_current_user(), which lives in pluggable.php and isn't loaded yet during the very earliest stages of the WordPress bootstrap. Calling it before that file is included fails.

Should I use wp_get_current_user() or get_current_user_id() to check who's logged in?

get_current_user_id() is listed in core as using wp_get_current_user() internally and just returns the ->ID property, so it does strictly less work when you only need the number.

Alternatives and related functions

get_current_user_id
When you only need the numeric user ID, not the full profile data.
is_user_logged_in
When all you need is a true or false answer about whether a visitor is authenticated.
get_userdata
When you need the WP_User object for a specific user ID rather than whoever is currently logged in.
current_user_can
When the goal is a capability check rather than reading properties off the user object yourself.

Performance profile

How much work a call to wp_get_current_user() 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
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

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

Instructions
3

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
40

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

What it touches

  • hookthird-party callbacksapply_filters()one call below wp_get_current_user()

Further down the call graph this can also reach query. That is the worst case, several calls deep and usually down an error path, not what a normal call pays.

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 wp_get_current_user() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always3_wp_get_current_user()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 3 instructions, 3 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 · 40

Show all 40

Source code

	function wp_get_current_user() {		return _wp_get_current_user();	}

Changelog

Introduced in 2.0.3. 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 6.7.7 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.