WordPress - Check if user is logged in

WordPress - Check if user is logged in

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

WordPress - Check if user is logged in

A common requirement for custom themes or child themes is conditional rendering of navigation elements based on authentication status. In a typical setup, you might be working inside header.php and attempting to display a specific menu only when a visitor has authenticated credentials. A frequent stumbling block for developers new to the WordPress core is relying on non-existent functions like is_logged_in(). This function does not exist in the standard WordPress API, which often leads to silent failures where your conditional logic never triggers as expected.

The correct approach relies on WordPress's built-in global variable $current_user or, more robustly, the utility functions provided by the WP_User class. To check if a user is logged in, you should use is_user_logged_in(). This function returns a boolean value: true if an authenticated session exists and false otherwise.

Here is how you can implement this check directly within your header.php file:

<?php
if ( is_user_logged_in() ) {
    // Display the navigation for logged-in users
    wp_nav_menu( array(
        'theme_location' => 'logged-in-menu',
        'fallback_cb'    => false,
    ) );
} else {
    // Optionally display a different menu or nothing at all
    wp_nav_menu( array(
        'theme_location' => 'guest-menu',
        'fallback_cb'    => false,
    ) );
}
?>

It is important to understand the underlying mechanics of why is_user_logged_in() works while custom functions like is_logged_in() do not. WordPress maintains a session cookie on the client side that stores the authentication state. When this function is called, it queries the session data and the wp_users table via the global $current_user object. If the session is valid and the user exists in the database, the function returns true.

There are specific trade-offs to consider when implementing conditional logic in your theme header. One significant concern is performance. While checking a boolean flag is computationally cheap, repeatedly calling functions that fetch heavy data can degrade page speed. In modern WordPress versions, is_user_logged_in() is optimized and does not load the full user object unless explicitly requested, making it safe for use in high-traffic loops or headers.

However, if you need to access specific user data—such as their role or ID—you must ensure that get_currentuserinfo() has been called or, preferably, that the current user object is initialized using wp_get_current_user(). In recent versions of WordPress (5.0+), this initialization happens automatically upon page load, but relying on legacy functions can cause confusion.

Another pitfall involves caching plugins. If your site uses aggressive server-side or object caching, you might inadvertently cache the result of an is_user_logged_in() check that was true for an admin user and serve it to a guest. While this is rare with standard logic, it is something to keep in mind when building complex conditional templates.

For developers looking to manage more complex access control scenarios beyond simple login states, exploring role-based checks using current_user_can() is often the next logical step. This allows you to restrict navigation not just by authentication status, but by specific capabilities assigned to user roles.

If your theme architecture requires a more decoupled approach where authentication logic lives outside the theme templates, consider implementing a custom REST API endpoint. This allows your frontend application to query the login state via JSON without rendering PHP directly in the view layer, promoting separation of concerns. For teams transitioning from monolithic PHP applications to modern stack architectures, this pattern aligns well with the methodologies discussed at https://laravelcompany.com regarding backend decoupling strategies.

Ultimately, sticking to the core WordPress API functions like is_user_logged_in() ensures compatibility across themes and plugins without requiring custom database queries or session manipulation.

Our network