is_user_logged_in() - WordPress 函数参考中文文档

返回介绍

is_user_logged_in()

发布于 2017-09-11 字数6452 浏览 1022 评论 0

is_user_logged_in()

Checks if the current visitor is a logged in user.


描述


返回值

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


源代码

File: wp-includes/pluggable.php

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

	return $user->exists();
}

更新日志

Version 描述
2.0.0 Introduced.

相关函数

Uses

  • wp-includes/pluggable.php:
    wp_get_current_user()

Used By

  • wp-includes/rest-api.php:
    rest_authorization_required_code()
  • wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php:
    WP_REST_Comments_Controller::create_item_permissions_check()
  • wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php:
    WP_REST_Comments_Controller::create_item()
  • wp-includes/comment.php:
    wp_check_comment_flood()
  • wp-includes/rest-api.php:
    rest_cookie_check_errors()
  • wp-includes/rest-api/class-wp-rest-server.php:
    WP_REST_Server::serve_request()
  • wp-signup.php:
    validate_another_blog_signup()
  • wp-signup.php:
    show_blog_form()
  • wp-signup.php:
    validate_blog_form()
  • wp-admin/includes/ms.php:
    _access_denied_splash()
  • wp-includes/class-wp-customize-manager.php:
    WP_Customize_Manager::save()
  • wp-includes/class-wp-customize-manager.php:
    WP_Customize_Manager::setup_theme()
  • wp-includes/general-template.php:
    wp_heartbeat_settings()
  • wp-includes/general-template.php:
    user_can_richedit()
  • wp-includes/general-template.php:
    wp_loginout()
  • wp-includes/general-template.php:
    wp_register()
  • wp-includes/class-wp.php:
    WP::send_headers()
  • wp-includes/class-wp-query.php:
    WP_Query::get_posts()
  • wp-includes/class-wp-query.php:
    WP_Query::parse_search()
  • wp-includes/functions.php:
    wp_auth_check()
  • wp-includes/functions.php:
    wp_auth_check_load()
  • wp-includes/link-template.php:
    get_adjacent_post()
  • wp-includes/class-wp-admin-bar.php:
    WP_Admin_Bar::_render()
  • wp-includes/class-wp-admin-bar.php:
    WP_Admin_Bar::initialize()
  • wp-includes/admin-bar.php:
    wp_admin_bar_site_menu()
  • wp-includes/admin-bar.php:
    wp_admin_bar_my_sites_menu()
  • wp-includes/admin-bar.php:
    is_admin_bar_showing()
  • wp-includes/post-template.php:
    get_body_class()
  • wp-includes/post.php:
    get_posts_by_author_sql()
  • wp-includes/post.php:
    _count_posts_cache_key()
  • wp-includes/post.php:
    wp_count_posts()
  • wp-includes/comment-template.php:
    get_post_reply_link()
  • wp-includes/comment-template.php:
    wp_list_comments()
  • wp-includes/comment-template.php:
    comment_form()
  • wp-includes/comment-template.php:
    get_comment_reply_link()
  • wp-includes/class-wp-customize-widgets.php:
    WP_Customize_Widgets::wp_ajax_update_widget()
  • Show 31 more used by
    Hide more used by


User Contributed Notes

  1. Skip to note content

    You must log in to vote on the helpfulness of this noteVote results for this note: 5You must log in to vote on the helpfulness of this note

    Contributed by Codex

    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. 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' );
    

    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');

    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 } ?>
    

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

上一篇:is_user_admin()

下一篇:is_user_member_of_blog()

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!