WordPress is_user_logged_in() Function

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.
if ( is_user_logged_in() ) {
// code for the true path
} else {
// code for the false path
}
| Call | What 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
is_user_logged_in(): bool
Parameters
| Parameter | Description |
|---|
| None | This function takes no parameters. |
Return Value
| Return Value | Description |
|---|
| bool | true 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.
<?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>';
} ?>
Logged in as Jane (true):
Welcome back, Jane!
Guest (false):
<a href="https://example.com/wp-login.php">Log in</a>
How It Works
is_user_logged_in() checks the login.wp_get_current_user() returns the user.- The
else branch shows a login link for guests.
Example 2 — Members-only content
Hide text from guests.
<?php if ( is_user_logged_in() ) {
echo '<p>Download the PDF guide.</p>';
} else {
echo '<p>Please log in to download the guide.</p>';
} ?>
Logged in (true):
<p>Download the PDF guide.</p>
Guest (false):
<p>Please log in to download the guide.</p>
How It Works
- The
if branch is for logged-in users. - Guests see the other message.
- Any role counts, even Subscriber.
Example 3 — Redirect guests
Send guests to the login page.
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' );
Guest opens /members/ (redirect):
Redirect to https://example.com/wp-login.php?redirect_to=...
Logged-in user opens /members/:
(the page is shown)
How It Works
template_redirect runs after the query is set.is_page() checks the Page and ! is_user_logged_in() checks for a guest.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.
About the author
Developer, cloud engineer, and technical writer
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