VOOZH about

URL: https://developer.wordpress.org/reference/functions/is_user_logged_in/

⇱ is_user_logged_in() – Function | Developer.WordPress.org


Skip to content

WordPress Developer Resources

is_user_logged_in()

HomeReferenceFunctionsis_user_logged_in()

is_user_logged_in(): bool

Determines whether the current visitor is a logged in user.

Description

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

Return

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

Source

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

	return $user->exists();
}

View all references View on Trac View on GitHub

Related

UsesDescription
wp_get_current_user()wp-includes/pluggable.php

Retrieves the current user object.

Used byDescription
wp_get_speculation_rules_configuration()wp-includes/speculative-loading.php

Returns the speculation rules configuration.

wp_register_core_abilities()wp-includes/abilities.php

Registers the default core abilities.

build_comment_query_vars_from_block()wp-includes/blocks.php

Helper function that constructs a comment query vars array from the passed block properties.

locate_block_template()wp-includes/block-template.php

Finds a block template with equal or higher specificity than a given PHP template file.

get_the_block_template_html()wp-includes/block-template.php

Returns the markup for the current template.

WP_REST_Application_Passwords_Controller::get_user()wp-includes/rest-api/endpoints/class-wp-rest-application-passwords-controller.php

Gets the requested user.

WP_Recovery_Mode::handle_exit_recovery_mode()wp-includes/class-wp-recovery-mode.php

Handles a request to exit Recovery Mode.

WP_Site_Health::check_wp_version_check_exists()wp-admin/includes/class-wp-site-health.php

Tests whether wp_version_check is blocked.

WP_Customize_Manager::handle_dismiss_autosave_or_lock_request()wp-includes/class-wp-customize-manager.php

Deletes a given auto-draft changeset or the autosave revision for a given changeset or delete changeset lock.

WP_Customize_Manager::handle_changeset_trash_request()wp-includes/class-wp-customize-manager.php

Handles request to trash a changeset.

wp_enqueue_code_editor()wp-includes/general-template.php

Enqueues assets needed by the code editor for the given settings.

rest_authorization_required_code()wp-includes/rest-api.php

Returns a contextual HTTP error code for authorization failure.

WP_Customize_Manager::changeset_data()wp-includes/class-wp-customize-manager.php

Gets changeset data.

WP_REST_Comments_Controller::create_item_permissions_check()wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php

Checks if a given request has access to create a comment.

WP_REST_Comments_Controller::create_item()wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php

Creates a comment.

wp_check_comment_flood()wp-includes/comment.php

Checks whether comment flooding is occurring.

rest_send_cors_headers()wp-includes/rest-api.php

Sends Cross-Origin Resource Sharing headers with API requests.

rest_cookie_check_errors()wp-includes/rest-api.php

Checks for errors when using cookie-based authentication.

WP_Customize_Manager::customize_pane_settings()wp-includes/class-wp-customize-manager.php

Prints JavaScript settings for parent window.

WP_REST_Server::serve_request()wp-includes/rest-api/class-wp-rest-server.php

Handles serving a REST API request.

retrieve_password()wp-includes/user.php

Handles sending a password retrieval email to a user.

validate_another_blog_signup()wp-signup.php

Validates a new site sign-up for an existing user.

show_blog_form()wp-signup.php

Generates and displays the Sign-up and Create Site forms.

validate_blog_form()wp-signup.php

Validates the new site sign-up.

_access_denied_splash()wp-admin/includes/ms.php

Displays an access denied message when a user tries to view a site’s dashboard they do not have access to.

WP_Customize_Manager::save()wp-includes/class-wp-customize-manager.php

Handles customize_save WP Ajax request to save/update a changeset.

WP_Customize_Manager::setup_theme()wp-includes/class-wp-customize-manager.php

Starts preview and customize theme.

wp_heartbeat_settings()wp-includes/general-template.php

Default settings for heartbeat.

user_can_richedit()wp-includes/general-template.php

Determines whether the user can access the visual editor.

wp_loginout()wp-includes/general-template.php

Displays the Log In/Out link.

wp_register()wp-includes/general-template.php

Displays the Registration or Admin link.

WP::send_headers()wp-includes/class-wp.php

Sends additional HTTP headers for caching, content type, etc.

WP_Query::get_posts()wp-includes/class-wp-query.php

Retrieves an array of posts based on query variables.

WP_Query::parse_search()wp-includes/class-wp-query.php

Generates SQL for the WHERE clause based on passed search terms.

wp_auth_check()wp-includes/functions.php

Checks whether a user is still logged in, for the heartbeat.

wp_auth_check_load()wp-includes/functions.php

Loads the auth check for monitoring whether the user is still logged in.

get_adjacent_post()wp-includes/link-template.php

Retrieves the adjacent post.

WP_Admin_Bar::initialize()wp-includes/class-wp-admin-bar.php

Initializes the admin bar.

wp_admin_bar_site_menu()wp-includes/admin-bar.php

Adds the “Site Name” menu.

wp_admin_bar_my_sites_menu()wp-includes/admin-bar.php

Adds the “My Sites/[Site Name]” menu and all submenus.

is_admin_bar_showing()wp-includes/admin-bar.php

Determines whether the admin bar should be showing.

get_body_class()wp-includes/post-template.php

Retrieves an array of the class names for the body element.

get_posts_by_author_sql()wp-includes/post.php

Retrieves the post SQL based on capability, author, and type.

_count_posts_cache_key()wp-includes/post.php

Returns the cache key for wp_count_posts() based on the passed arguments.

wp_count_posts()wp-includes/post.php

Counts number of posts of a post type and if user has permissions to view.

get_post_reply_link()wp-includes/comment-template.php

Retrieves HTML content for reply to post link.

wp_list_comments()wp-includes/comment-template.php

Displays a list of comments.

comment_form()wp-includes/comment-template.php

Outputs a complete commenting form for use within a template.

comments_template()wp-includes/comment-template.php

Loads the comment template specified in $file.

get_comment_reply_link()wp-includes/comment-template.php

Retrieves HTML content for reply to comment link.

WP_Customize_Widgets::wp_ajax_update_widget()wp-includes/class-wp-customize-widgets.php

Updates widget settings asynchronously.

get_page_of_comment()wp-includes/comment.php

Calculates what page number a comment will appear on for comment paging.

Show 47 moreShow less

Changelog

VersionDescription
2.0.0Introduced.

User Contributed Notes

  1. Skip to note 7 content

    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!';
    }
    ?>
  2. Skip to note 8 content

    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');
  3. Skip to note 9 content

    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 10 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' );
  5. Skip to note 11 content
    /**
     * Redirect to Home page if user attempts to try go to login if logged in
     * @author Arslan <arslan@wpbrigade.com>
     * @return void
     */
    function redirect_to() {
    	global $pagenow;
    
    	if ( !is_customize_preview() && is_user_logged_in() && 'index.php' !== $pagenow ) {
    		wp_redirect( home_url(), 302 );
    		exit();
    	}
    }
  6. Skip to note 12 content

    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.