is_user_logged_in(): bool
- Since
- 2.0.0
- Source
wp-includes/pluggable.php:1177
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.
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?
- Why does is_user_logged_in() return true for a subscriber when I expected only admins?
- Why does is_user_logged_in() report false for every visitor when full-page caching is on?
- Is is_user_logged_in() the same as is_admin()?
Why does is_user_logged_in() throw a fatal error when I call it early?
Why does is_user_logged_in() return true for a subscriber when I expected only admins?
Why does is_user_logged_in() report false for every visitor when full-page caching is on?
Is is_user_logged_in() the same as is_admin()?
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
- Scaling
- Constant
- Instructions
- 6
- Plugin surface
- None
- Called by
- 50
Touches nothing outside its own arguments.
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 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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 6 | wp_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
- wp_get_current_user()Retrieves the current user object.
Used by · 50
- WP::send_headers()Sends additional HTTP headers for caching, content type, etc.
- WP_Admin_Bar::initialize()Initializes the admin bar.
- WP_Customize_Manager::changeset_data()Gets changeset data.
- WP_Customize_Manager::customize_pane_settings()Prints JavaScript settings for parent window.
- WP_Customize_Manager::handle_changeset_trash_request()Handles request to trash a changeset.
- WP_Customize_Manager::handle_dismiss_autosave_or_lock_request()Deletes a given auto-draft changeset or the autosave revision for a given changeset or delete changeset lock.
- WP_Customize_Manager::save()Handles customize_save WP Ajax request to save/update a changeset.
- WP_Customize_Manager::setup_theme()Starts preview and customize theme.
- WP_Customize_Widgets::wp_ajax_update_widget()Updates widget settings asynchronously.
- WP_Query::get_posts()Retrieves an array of posts based on query variables.
- WP_Query::parse_search()Generates SQL for the WHERE clause based on passed search terms.
- WP_REST_Application_Passwords_Controller::get_user()Gets the requested user.
Show all 50
- WP_REST_Comments_Controller::create_item()Creates a comment.
- WP_REST_Comments_Controller::create_item_permissions_check()Checks if a given request has access to create a comment.
- WP_REST_Server::serve_request()Handles serving a REST API request.
- WP_Recovery_Mode::handle_exit_recovery_mode()Handles a request to exit Recovery Mode.
- WP_Site_Health::check_wp_version_check_exists()Tests whether `wp_version_check` is blocked.
- _access_denied_splash()Displays an access denied message when a user tries to view a site's dashboard they do not have access to.
- _count_posts_cache_key()Returns the cache key for wp_count_posts() based on the passed arguments.
- build_comment_query_vars_from_block()Helper function that constructs a comment query vars array from the passed block properties.
- comment_form()Outputs a complete commenting form for use within a template.
- comments_template()Loads the comment template specified in $file.
- get_adjacent_post()Retrieves the adjacent post.
- get_body_class()Retrieves an array of the class names for the body element.
- get_comment_reply_link()Retrieves HTML content for reply to comment link.
- get_page_of_comment()Calculates what page number a comment will appear on for comment paging.
- get_post_reply_link()Retrieves HTML content for reply to post link.
- get_posts_by_author_sql()Retrieves the post SQL based on capability, author, and type.
- get_the_block_template_html()Returns the markup for the current template.
- is_admin_bar_showing()Determines whether the admin bar should be showing.
- locate_block_template()Finds a block template with equal or higher specificity than a given PHP template file.
- render_block_core_calendar()Renders the `core/calendar` block on server.
- render_block_core_loginout()Renders the `core/loginout` block on server.
- rest_authorization_required_code()Returns a contextual HTTP error code for authorization failure.
- rest_cookie_check_errors()Checks for errors when using cookie-based authentication.
- rest_send_cors_headers()Sends Cross-Origin Resource Sharing headers with API requests.
- retrieve_password()Handles sending a password retrieval email to a user.
- show_blog_form()Generates and displays the Sign-up and Create Site forms.
- user_can_richedit()Determines whether the user can access the visual editor.
- validate_another_blog_signup()Validates a new site sign-up for an existing user.
- validate_blog_form()Validates the new site sign-up.
- wp_admin_bar_my_sites_menu()Adds the "My Sites/[Site Name]" menu and all submenus.
- wp_admin_bar_site_menu()Adds the "Site Name" menu.
- wp_auth_check()Checks whether a user is still logged in, for the heartbeat.
- wp_auth_check_load()Loads the auth check for monitoring whether the user is still logged in.
- wp_check_comment_flood()Checks whether comment flooding is occurring.
- wp_count_posts()Counts number of posts of a post type and if user has permissions to view.
- wp_enqueue_code_editor()Enqueues assets needed by the code editor for the given settings.
- wp_get_speculation_rules_configuration()Returns the speculation rules configuration.
- wp_heartbeat_settings()Default settings for heartbeat.
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.
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.