is_user_logged_in ()

Checks if the current visitor is a logged in user.

Return: (bool) True if user is logged in, false if not logged in.

Source file: wp-includes/pluggable.php

View source


Used by


Source

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

	return $user->exists();
}

Changelog


User Contributed Notes

  1. This function is more accurate if used at, or after, the ‘template_redirect’ Action. Before that, under unusual circumstances, it will give unexpected results.

    The most common case is for a Site Address (URL) without a “www.” when an http:// request is received with a “www.” specified.

    Background: between the ‘wp’ and ‘template_redirect’ Action, the Rewrite rules are applied for Pretty Permalinks. During this process, $_SERVER[‘SERVER_NAME’] is corrected in the common case listed above by removing “www.”

  2. Example: Display different output depending on whether the user is logged in or not.

    <?php
    if ( is_user_logged_in() ) {
    	echo 'Welcome, registered user!';
    } else {
    	echo 'Welcome, visitor!';
    }
    ?>
    
  3. Example: From your functions file, this code displays a personal message for logged in users.

    /**
     * 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!' );
    	}
    }
    add_action( 'loop_start', 'wpdocs_personal_message_when_logged_in' );
    

You must log in before being able to contribute a note.