函数文档

get_currentuserinfo()

💡 云策文档标注

概述

get_currentuserinfo() 是一个已弃用的 WordPress 函数,用于将当前登录用户的信息填充到全局变量中。自 WordPress 4.5.0 起,建议使用 wp_get_current_user() 替代。

关键要点

  • 函数已弃用:从 WordPress 4.5.0 版本开始,get_currentuserinfo() 被标记为弃用,应改用 wp_get_current_user()。
  • 功能作用:该函数将当前登录用户的详细信息(如用户名、邮箱、ID 等)存储到全局变量中,便于通过 WP_User 对象访问。
  • 返回值:在 XMLRPC 请求或无效认证 cookie 时返回 false,否则返回 WP_User 实例。
  • 相关函数:内部调用 _wp_get_current_user() 来获取用户对象,并使用 _deprecated_function() 标记弃用状态。

代码示例

global $current_user;
get_currentuserinfo();
printf( __( 'Username: %s', 'textdomain' ), esc_html( $current_user->user_login ) ) . '';
printf( __( 'User email: %s', 'textdomain' ), esc_html( $current_user->user_email ) ) . '';
printf( __( 'User first name: %s', 'textdomain' ), esc_html( $current_user->user_firstname ) ) . '';
printf( __( 'User last name: %s', 'textdomain' ), esc_html( $current_user->user_lastname ) ) . '';
printf( __( 'User display name: %s', 'textdomain' ), esc_html( $current_user->display_name ) ) . '';
printf( __( 'User ID: %s', 'textdomain' ), esc_html( $current_user->ID ) );

注意事项

  • 由于该函数已弃用,新开发中应避免使用,转而使用 wp_get_current_user() 来获取当前用户对象。
  • 在 XMLRPC 请求或认证失败的情况下,函数可能返回 false,需进行错误处理。

📄 原文内容

Populate global variables with information about the currently logged in user.

Description

See also

Return

bool|WP_User False on XMLRPC Request and invalid auth cookie, WP_User instance otherwise.

Source

function get_currentuserinfo() {
	_deprecated_function( __FUNCTION__, '4.5.0', 'wp_get_current_user()' );

	return _wp_get_current_user();
}

Changelog

Version Description
4.5.0 Deprecated. Use wp_get_current_user()
0.71 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Default Usage
    The call to get_currentuserinfo() places the current user’s info into $userdata, where it can be retrieved using member variables.

    user_login ) ) . '<br />';
    printf( __( 'User email: %s', 'textdomain' ), esc_html( $current_user->user_email ) ) . '<br />';
    printf( __( 'User first name: %s', 'textdomain' ), esc_html( $current_user->user_firstname ) ) . '<br />';
    printf( __( 'User last name: %s', 'textdomain' ), esc_html( $current_user->user_lastname ) ) . '<br />';
    printf( __( 'User display name: %s', 'textdomain' ), esc_html( $current_user->display_name ) ) . '<br />';
    printf( __( 'User ID: %s', 'textdomain' ), esc_html( $current_user->ID ) );

    Result:

    Username: Zedd
    User email: my@email.com
    User level: 10
    User first name: John
    User last name: Doe
    User display name: John Doe
    User ID: 1