is_user_logged_in()WP 2.0.0

Checks if the current visitor is a logged in user.

For more information on this and similar theme functions, check out the Conditional Tags article in the Theme Developer Handbook.

Pluggable function — this function can be replaced from a plugin. It means that this function is defined (works) only after all plugins are loaded (included), but before this moment this function has not defined. Therefore, you cannot call this and all functions depended on this function directly from a plugin code. They need to be called on plugins_loaded hook or later, for example on init hook.

Function replacement (override) — in must-use or regular plugin you can create a function with the same name, then it will replace this function.

1 time — 0.000012 sec (very fast) | 50000 times — 0.01 sec (speed of light) | PHP 7.0.2, WP 4.4.1

No Hooks.

Return

true|false. True if user is logged in, false if not logged in.

Usage

is_user_logged_in();

Examples

0

#1 Check whether the user is logged in or not:

And display different text:

if ( is_user_logged_in() ) {
	echo 'You are logged in!';
}
else {
	echo 'You are NOT logged in!';
}
0

#2 Don't call the functions too early

is_user_logged_in() is a pluggable function and you could get a fatal error if you call it too early.

The best moment when you can use it is the hook init:

add_action( 'init', 'example_function' );

function example_function(){

	if ( is_user_logged_in() ) {
		// code
	}
}
0

#3 add login and logout link in the template

<?php if ( is_user_logged_in() ) { ?>
	<a href="<?php echo wp_logout_url(); ?>">Logout</a>
<?php } else { ?>
	<a href="/wp-login.php" title="Members Area Login" rel="home">Members Area</a>
<?php } ?>
0

#4 Displays a personal message for logged in users

Example: From your functions file:

add_action( 'loop_start', 'wpdocs_personal_message_when_logged_in' );

/**
 * Give a personalized message for logged in users and a generic one for anonymous visitors
 */
function wpdocs_personal_message_when_logged_in() {

	if ( is_user_logged_in() ) {
		$current_user = wp_get_current_user();
		printf( 'Personal Message For %s!', esc_html( $current_user->user_firstname ) );
	}
	else {
		echo( 'Non-Personalized Message!' );
	}
}
0

#5 Redirect to Home page if logged user try to go to login page

/**
 * Redirect to Home page if logged user try to go to login page.

 * @author Arslan <[email protected]>
 * @return void
 */
function redirect_to() {
	global $pagenow;

	if ( !is_customize_preview() && is_user_logged_in() && 'index.php' !== $pagenow ) {
		wp_redirect( home_url(), 302 );
		exit();
	}
}

Changelog

Since 2.0.0 Introduced.

is_user_logged_in() code WP 6.4.3

function is_user_logged_in() {
	$user = wp_get_current_user();

	return $user->exists();
}