is_user_logged_in()

Checks if the current visitor is a logged in user.


Description Description


Return Return

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


Top ↑

Source Source

File: wp-includes/pluggable.php

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

	return $user->exists();
}

Top ↑

Changelog Changelog

Changelog
Version Description
2.0.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note content
    Contributed by Brad Dalton

    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' );
    
  2. Skip to note content
    Contributed by jonradio

    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.”

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