is_user_logged_in()

Determines whether the current visitor is a logged in user.


Description Description

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


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 2 content
    Contributed by Marco Panichi

    Please note that is_user_logged_in is a pluggable function and you could get a fatal error if you call it too early.

    To solve this problem, you can wrap the login check within a function hooked to the init action:

    function example_function()
    {
    	if ( is_user_logged_in() ) 
    	{
    		// code
    	}
    }
    add_action('init', 'example_function');
  2. Skip to note 3 content

    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' );
    
  3. Skip to note 4 content
    Contributed by Neil Bannet

    Example: In case somebody want to add login and logout link in the template somewhere.

     		
    <?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 } ?>
    
  4. Skip to note 5 content
    Contributed by joncomputing

    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.