WordPress is_user_logged_in() Function

Beginner
4 min read
Updated: Nov 2026
3 examples
Conditional tag

Definition

In WordPress, is_user_logged_in() returns true when the visitor is logged in to the site, with any role. It returns false for guests.

Use it to show different content to members and guests, such as a Log in link or a Welcome back message.

Syntax

Use it as the test of an if statement.

WordPress
if ( is_user_logged_in() ) {
  // code for the true path
} else {
  // code for the false path
}
CallWhat it does
is_user_logged_in()True if the visitor is logged in.
! is_user_logged_in()True for guests.
current_user_can( 'edit_posts' )Use this to check a permission, not just a login.

It works only after WordPress has loaded the user, so use it in templates or on hooks such as init or later.

Full declaration
WordPress
is_user_logged_in(): bool

Parameters

ParameterDescription
NoneThis function takes no parameters.

Return Value

Return ValueDescription
booltrue if a user is logged in, otherwise false.

WordPress Examples

Three short theme examples. Each one shows what prints on the true path and the false path.

Example 1 — Welcome or log in

Show a different message for members and guests.

WordPress
<?php if ( is_user_logged_in() ) {
  $user = wp_get_current_user();
  echo 'Welcome back, ' . esc_html( $user->display_name ) . '!';
} else {
  echo '<a href="' . esc_url( wp_login_url() ) . '">Log in</a>';
} ?>

How It Works

  1. is_user_logged_in() checks the login.
  2. wp_get_current_user() returns the user.
  3. The else branch shows a login link for guests.

Example 2 — Members-only content

Hide text from guests.

WordPress
<?php if ( is_user_logged_in() ) {
  echo '<p>Download the PDF guide.</p>';
} else {
  echo '<p>Please log in to download the guide.</p>';
} ?>

How It Works

  1. The if branch is for logged-in users.
  2. Guests see the other message.
  3. Any role counts, even Subscriber.

Example 3 — Redirect guests

Send guests to the login page.

WordPress
function mytheme_members_only() {
  if ( is_page( 'members' ) && ! is_user_logged_in() ) {
    wp_safe_redirect( wp_login_url( get_permalink() ) );
    exit;
  }
}
add_action( 'template_redirect', 'mytheme_members_only' );

How It Works

  1. template_redirect runs after the query is set.
  2. is_page() checks the Page and ! is_user_logged_in() checks for a guest.
  3. wp_login_url() sends the guest back after login.

Frequently Asked Questions

It returns true or false (a bool). Use it inside an if statement.
No. It only says that a user is logged in. Use current_user_can() to check a role or capability.
No. is_admin() checks if you are in the dashboard. is_user_logged_in() checks the visitor login.
A full-page cache can show the same page to everyone. Exclude logged-in users from the cache, or load personal parts with JavaScript.

Next: is_year()

Check if the current page is a yearly archive.

is_year() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful