wppaste
WordPress

is_user_logged_in(): bool

Since
2.0.0
Source
wp-includes/pluggable.php:1262

Checks whether the visitor making the current request is signed in to WordPress, based on the global current user object. It returns a plain boolean with no arguments to configure, so it is the standard guard for logged-in-only markup, redirects, and conditional queries. It does not check capabilities or roles, only whether a user is authenticated at all; pair it with current_user_can() when a specific permission matters.

Determines whether the current visitor is a logged in user.

Description

For more information on this and similar theme functions, check out the https://developer.wordpress.org/themes/basics/conditional-tags/ Conditional Tags article in the Theme Developer Handbook.

Compatibility

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

bool
True if user is logged in, false if not 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 different content to logged-in visitors vs guests

Print one of two messages depending on whether the current request belongs to an authenticated user.

if ( is_user_logged_in() ) {
	echo esc_html( 'Welcome back, you are signed in.' );
} else {
	echo esc_html( 'Please log in to see member content.' );
}

The sandbox has an administrator logged in by default, so this prints the logged-in branch.

Prepend a members-only notice to post content

Hook into the_content to add an extra paragraph only for logged-in visitors viewing a single post.

add_filter( 'the_content', function( $content ) {
	if ( is_singular( 'post' ) && is_user_logged_in() ) {
		$notice = '<p><strong>' . esc_html__( 'Members note: your saved drafts are in the dashboard.', 'wppaste' ) . '</strong></p>';
		return $notice . $content;
	}

	return $content;
} );

Visit post ID 1 ("Hello world!") on the front end to see the notice prepended above the usual content.

Common problems and fixes · 4

Why does is_user_logged_in() throw a fatal error when I call it early?

The function delegates to wp_get_current_user(), which lives in wp-includes/pluggable.php and is not loaded until the plugins_loaded action has fired. Calling it from a must-use plugin, an early add_action('muplugins_loaded', ...) callback, or the top of a plugin file runs it before that function exists.

Why does is_user_logged_in() return true for a subscriber when I expected only admins?

It only checks whether $user->exists() is true, meaning some account is authenticated at all. It has no idea about roles or capabilities, so any logged-in user, regardless of role, makes it return true.

Why does is_user_logged_in() report false for every visitor when full-page caching is on?

A caching plugin or reverse proxy that serves the same generated HTML to anonymous requests never re-runs this PHP check per visitor, so a cached page baked from a logged-out request will show logged-out output to everyone until the cache is bypassed for authenticated sessions.

Is is_user_logged_in() the same as is_admin()?

No. is_admin() reports whether the current request is inside wp-admin, not whether a user is authenticated. A logged-out visitor viewing wp-login.php or a logged-in editor viewing the front end can both make is_admin() and is_user_logged_in() return different combinations of true and false.

Alternatives and related functions

wp_get_current_user
When you need the actual WP_User object, not just a yes-or-no answer, for example to read the user's login, email, or roles.
current_user_can
When the decision depends on a specific capability or role rather than mere authentication.
get_current_user_id
When all you need is the numeric ID of the logged-in user (0 when nobody is logged in) for something like a meta lookup.
is_admin
When you need to know whether the request is inside the wp-admin area, which is unrelated to login state.

Performance profile

How much work a call to is_user_logged_in() 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
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 one call costs · 1 distinct outcome

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

WhenInstructionsCalls it makes
always6wp_get_current_user(), ->exists()

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 is_user_logged_in() {		$user = wp_get_current_user(); 		return $user->exists();	}

Changelog

Introduced in 2.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 7.0.4 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.