Function and usage of is_user_logged_in() in wordpress


Answers ( 1 )


    The is_user_logged_in() function in WordPress is a simple, yet essential function used to determine if the current visitor to a WordPress site is a logged-in user. Here's a detailed explanation and a sample usage:

    Function Signature:

    is_user_logged_in(): bool

    Function Purpose:

    • Determines whether the current visitor is a logged-in user.
    • This function is crucial for theme developers and plugin developers who want to display different content or features based on the user's login status.


    • The is_user_logged_in() function checks the login status of the current visitor.
    • It is part of WordPress's Conditional Tags, which are used to change what content is displayed based on certain conditions.
    • This function is particularly useful in WordPress themes and plugins where certain content or features should only be accessible to logged-in users, such as profile information, dashboard access, or restricted posts.

    Return Value:

    • Type: bool
    • Value: Returns true if the user is logged in, and false if not logged in.

    Sample Usage:

    Example 1: Displaying Different Content Based on Login Status

    if ( is_user_logged_in() ) {
        echo 'Welcome, registered user!';
    } else {
        echo 'Welcome, guest!';

    In this example, the website will display a personalized welcome message to users who are logged in and a generic welcome message to guests.

    Example 2: Restricting Access to Content

    if ( is_user_logged_in() ) {
        // Show the content
    } else {
        echo 'Please log in to view this content.';

    Here, the content of a post or page is only displayed to logged-in users. Guests are prompted to log in to view the content.

    Example 3: Using in Theme Templates

    Often used in WordPress theme templates (like header.php, sidebar.php) to show or hide elements:

    if ( is_user_logged_in() ) {
        // Display logout link
        echo '<a href="'. wp_logout_url() .'">Logout</a>';
    } else {
        // Display login link
        echo '<a href="'. wp_login_url() .'">Login</a>';

    In theme templates, this function helps in displaying login or logout links based on the user's status.

    Additional Information:

    • To learn more about is_user_logged_in() and similar theme functions, it's recommended to check out the Conditional Tags article in the WordPress Theme Developer Handbook.
    • This function is part of the WordPress core, so it's available in any standard WordPress installation without the need for additional plugins.

Leave an answer