wppaste
WordPress

get_current_user_id(): int

Since
MU (3.0.0)
Source
wp-includes/user.php:726

Returns the numeric ID of the currently logged-in user, or 0 when no one is signed in. It reads the value from wp_get_current_user() rather than querying the database directly, so it reflects whatever the global current user object holds at the moment it's called. Because 0 is a valid, non-error return for logged-out visitors, code that checks the result should not confuse it with a real error condition.

Gets the current user's ID.

Compatibility

WordPress
since MU (3.0.0)
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

int
The current user's ID, or 0 if no user is logged in.

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 a personalized greeting only to logged-in users

Print a different message depending on whether the current visitor has an ID.

$user_id = get_current_user_id();

if ( $user_id > 0 ) {
	echo esc_html( sprintf( 'Welcome back, user #%d.', $user_id ) );
} else {
	echo esc_html( 'Welcome, guest. Log in to see your account.' );
}

The sandbox has an administrator logged in, so this prints the welcome-back branch with a real ID.

Record which user last edited a post as post meta

Stamp an existing post with the ID of whoever is currently making the change, then read it back.

$post_id = 2;
$editor_id = get_current_user_id();

update_post_meta( $post_id, '_last_edited_by', $editor_id );

$stored = get_post_meta( $post_id, '_last_edited_by', true );

printf(
	'Post %d was last touched by user #%s.',
	absint( $post_id ),
	esc_html( $stored )
);

Common problems and fixes · 3

Why does get_current_user_id() return 0 even though I know I'm logged in?

The function checks function_exists( 'wp_get_current_user' ) before calling it, and returns 0 immediately if that pluggable function isn't defined yet. This happens when the code runs too early, such as in a must-use plugin or on a hook that fires before pluggable.php loads.

Is checking if (get_current_user_id()) a safe way to test login status?

It works because no real user ever has an ID of 0, but the function's job is only to return an ID, not to communicate intent, so the check reads ambiguously to anyone else maintaining the code.

Can I use the returned ID to decide if a user is allowed to do something?

No. The source only ever returns an ID (or 0), it never checks roles or capabilities, so a logged-in user with a low-privilege role still gets a non-zero ID here.

Alternatives and related functions

wp_get_current_user
When you need the full user object (email, roles, display name) rather than just the numeric ID.
is_user_logged_in
When all you need is a true/false answer about login status, without the ID itself.
current_user_can
When the goal is to check whether the current user has a specific capability, not just identify who they are.
get_userdata
When you need data for a user other than the one currently logged in, given their ID.

Performance profile

How much work a call to get_current_user_id() 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
5–12

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 14.

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 one call costs · 2 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost get_current_user_id() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!function_exists()5function_exists()
function_exists()10–12function_exists(), 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: 14 instructions, 5–12 executed per call, 2 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_current_user_id() {	if ( ! function_exists( 'wp_get_current_user' ) ) {		return 0;	}	$user = wp_get_current_user();	return ( isset( $user->ID ) ? (int) $user->ID : 0 );}

Changelog

Introduced in MU (3.0.0). 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.9.7 tag, from src/wp-includes/user.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.