函数文档

is_user_logged_in()

💡 云策文档标注

概述

is_user_logged_in() 是一个 WordPress 条件函数,用于判断当前访客是否为已登录用户。它返回布尔值,常用于主题或插件中根据用户登录状态显示不同内容。

关键要点

  • 函数返回布尔值:true 表示用户已登录,false 表示未登录。
  • 内部实现基于 wp_get_current_user() 和 $user->exists() 检查用户对象是否存在。
  • 这是一个可插拔函数(pluggable),调用过早可能导致致命错误,建议在 init 或 template_redirect 钩子后使用以确保准确性。
  • 广泛用于权限检查、个性化内容显示、登录/注销链接控制等场景。

代码示例

// 示例:根据登录状态显示不同消息
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' );

注意事项

  • 避免在插件或主题加载过早阶段调用,建议包装在 init 动作钩子中以防止错误。
  • 在 template_redirect 动作后使用可获得更准确的结果,特别是在处理 URL 重写和服务器名称时。
  • 函数依赖于 WordPress 用户系统,确保在正确上下文中使用。

📄 原文内容

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();
}

Changelog

Version Description
2.0.0 Introduced.

User Contributed Notes

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

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

  3. 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();
    	}
    }

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