How7o
  • Home
  • Tools
  • Prank Screens
  • Contact
  • Blog
Reading: How to Check If a User Is Logged In in WordPress
Share
Subscribe Now
How7oHow7o
Font ResizerAa
  • Marketing
  • OS
  • Features
  • Guide
  • Complaint
  • Advertise
Search
  • Home
  • Tools
  • Prank Screens
  • Contact
  • Blog
Follow US
Copyright © 2014-2023 Ruby Theme Ltd. All Rights Reserved.
How7o > Blog > Web Development > How to Check If a User Is Logged In in WordPress
Web Development

How to Check If a User Is Logged In in WordPress

how7o
By how7o
Last updated: May 10, 2026
7 Min Read
WordPress check if user is logged in with is_user_logged_in()
SHARE

To wordpress check user logged in inside a theme file — header.php to swap navigation, single.php to hide a form, a widget to show user-only content — reach for is_user_logged_in(). The function is a clean boolean, works anywhere in template code once WordPress has bootstrapped, and never hits the database. This guide covers the basic conditional, the one-line menu switch, and the common mistake of calling it too early.

Contents
  • TL;DR
  • The basic conditional
  • Switching nav menus by login state
  • Getting the logged-in user’s info
  • Don’t call it too early
  • Capability checks for gated features
  • Frequently asked questions
  • Related guides
  • References

Last verified: 2026-04-23 on WordPress 6.5 and PHP 8.3. Originally published 2022-06-16, rewritten and updated 2026-04-23.

TL;DR

if ( is_user_logged_in() ) {
    // code for logged-in users
} else {
    // code for guests
}

The basic conditional

Inside header.php (or any theme file) wrap the code that should differ between logged-in and guest users:

<?php if ( is_user_logged_in() ) : ?>
    <a href="<?php echo esc_url( wp_logout_url() ); ?>">Log out</a>
<?php else : ?>
    <a href="<?php echo esc_url( wp_login_url() ); ?>">Log in</a>
<?php endif; ?>

The function returns true when the request has a valid authentication cookie. Under the hood it checks the global $current_user — so by the time any template file runs, the value is already resolved and the call is essentially free.

Switching nav menus by login state

The one-line variant for swapping registered nav menus based on login state:

wp_nav_menu( array(
    'theme_location' => is_user_logged_in() ? 'logged-in-menu' : 'logged-out-menu',
) );

Both locations need to exist first — register them via register_nav_menus in functions.php. For the full menu-register + display walkthrough see Display Different Menus to Logged-In Users in WordPress.

wordpress check user logged in — is_user_logged_in branches header, menu, and REST responses

Getting the logged-in user’s info

if ( is_user_logged_in() ) {
    $user_id = get_current_user_id();   // int, or 0 if guest
    $user    = wp_get_current_user();   // WP_User object

    echo esc_html( $user->display_name );
    echo esc_html( $user->user_email );
}

get_current_user_id() is the lighter call — use it when you only need the ID (for a get_post_meta lookup, a conditional on post ownership). wp_get_current_user() returns the full object with email, display name, roles, and all meta. Both work on guests too — the ID becomes 0 and the object’s fields are empty.

Don’t call it too early

A common mistake is using is_user_logged_in() at the top of functions.php:

// functions.php — this WON'T work
if ( is_user_logged_in() ) {
    add_action( 'wp_head', 'my_custom_head' );
}

WordPress hasn’t resolved the current user by the time functions.php is loaded (that happens on init or later). The call always returns false. Wrap the conditional in an init or later hook:

add_action( 'init', function () {
    if ( is_user_logged_in() ) {
        add_action( 'wp_head', 'my_custom_head' );
    }
} );

Capability checks for gated features

For anything more specific than “is anyone logged in?” — admin menus, editor-only forms, custom dashboard widgets — use current_user_can:

if ( current_user_can( 'manage_options' ) ) {
    // admins only
}

if ( current_user_can( 'edit_posts' ) ) {
    // editors, authors, contributors, admins
}

if ( current_user_can( 'edit_post', $post_id ) ) {
    // the specific post's editor / owner
}

The third form — passing an object ID — is the ownership check most plugins get wrong. Always pass the post/user/term ID when the capability has a meta-capability form (edit_post, delete_post, read_post).

Frequently asked questions

What’s the simplest wordpress check user logged in call?

if ( is_user_logged_in() ) { ... }. The function returns a boolean — true when the current request has a valid auth cookie, false otherwise. It works anywhere in WordPress template code (header.php, footer.php, any template part, any theme function) and doesn’t hit the database — it reads the already-resolved global $current_user.

Can I use it outside a template, for example in functions.php?

Yes — but only after WordPress has initialized the current user, which happens on the init hook or later. Calling is_user_logged_in() from the top of functions.php (which runs earlier) returns false even for logged-in requests. Wrap any conditional logic in an add_action('init', ...) or later hook.

How do I get the logged-in user’s info?

wp_get_current_user() returns the WP_User object. For just the ID, get_current_user_id() returns an int (or 0 if not logged in). Use the ID form in conditions and the full object when you need the email, display name, or roles: $user = wp_get_current_user(); $user->display_name;.

What’s the difference between is_user_logged_in() and current_user_can()?

is_user_logged_in() only confirms that some authenticated user exists. current_user_can('capability') checks whether that user has a specific permission — edit_posts, manage_options, publish_posts, a custom cap. For any admin-only or role-gated feature, reach for current_user_can — the logged-in check is too coarse.

Can I check login status on the frontend with JavaScript?

Not directly — the auth cookie (wordpress_logged_in_*) is HttpOnly, so JS can’t read it. Either inject the status from PHP via wp_localize_script(..., ['isLoggedIn' => is_user_logged_in()]), or call an authenticated REST endpoint (/wp-json/wp/v2/users/me) and check the response. The first is simpler when the data is needed at page load; the second is the right answer when the frontend is a SPA making async calls.

Related guides

  • How to Display Different Menus to Logged-In Users in WordPress — the full register-menu + wp_nav_menu walkthrough.
  • How to Login a User Programmatically in WordPress — the sibling authentication pattern.
  • How to Search Users in WordPress — looking up users by username, email, first/last name.
  • How to Change a User Profile Picture in WordPress Without a Plugin — another logged-in-user customization.

References

WordPress developer reference for is_user_logged_in: developer.wordpress.org/reference/functions/is_user_logged_in. Roles and Capabilities: developer.wordpress.org/plugins/users/roles-and-capabilities.

TAGGED:authphpwordpress

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article WordPress login user programmatically — wp_set_current_user plus wp_set_auth_cookie How to Login a User Programmatically in WordPress
Next Article WordPress get current category ID — three methods by page context How to Get the Current Category ID in WordPress
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow

Subscribe Now

Subscribe to our newsletter to get our newest articles instantly!
Most Popular
Display PHP errors — ini_set + php.ini configuration
How to Display PHP Errors
May 10, 2026
PHP convert string to uppercase — strtoupper and mb_strtoupper
How to Convert a String to Uppercase in PHP
May 10, 2026
PHP string to float conversion with cast, regex cleanup, NumberFormatter
How to Convert a String to Float in PHP
May 10, 2026
PHP merge arrays without duplicates — union operator and array_unique
How to Combine Two Arrays Without Duplicates in PHP
May 10, 2026
PHP delete array element — unset, array_splice, array_filter, array_search
How to Delete an Element from a PHP Array
May 10, 2026

You Might Also Like

How I Fixed Composer Dependency Errors
Web Development

How I Fixed Composer Dependency Errors Using the –ignore-platform-reqs Flag (Step-by-Step Guide)

7 Min Read
Find prime numbers in JavaScript thumbnail
Web Development

How to Find Prime Numbers in JavaScript (1 to 100) — Fast & Simple Methods

5 Min Read
Laravel Eloquent current month records — calendar and query builder illustration
Web Development

How to Get Current Month Records in Laravel Eloquent

8 Min Read
Display only the current date in Laravel using Carbon
Web Development

How to Display Only the Current Date in Laravel (Carbon Examples)

4 Min Read
How7o

We provide tips, tricks, and advice for improving websites and doing better search.

Latest News

  • SEO Audit Tool
  • Client ReferralsNew
  • Execution of SEO
  • Reporting Tool

Resouce

  • Google Search Console
  • Google Keyword Planner
  • Google OptimiseHot
  • SEO Spider

Get the Top 10 in Search!

Looking for a trustworthy service to optimize the company website?
Request a Quote
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?