函数文档

_wp_get_current_user()

💡 云策文档标注

概述

_wp_get_current_user() 是 WordPress 核心函数,用于获取当前用户对象。它会设置当前用户,如果未设置则根据登录状态或过滤器确定用户,并返回 WP_User 实例。

关键要点

  • 函数返回当前用户的 WP_User 实例,如果未设置用户,会尝试设置当前用户。
  • 如果用户已登录,设置为该用户;否则设置为 ID 0(无效用户,无权限)。
  • 用于可插拔函数 wp_get_current_user() 和已弃用的 get_currentuserinfo(),后者用于向后兼容。
  • 包含一个过滤器 determine_current_user,允许通过 apply_filters 自定义用户确定逻辑。
  • 函数内部处理多种情况:如 $current_user 已设置、为 stdClass 对象或无效值,以及 XMLRPC 请求。

代码示例

function _wp_get_current_user() {
	global $current_user;

	if ( ! empty( $current_user ) ) {
		if ( $current_user instanceof WP_User ) {
			return $current_user;
		}

		// Upgrade stdClass to WP_User.
		if ( is_object( $current_user ) && isset( $current_user->ID ) ) {
			$cur_id       = $current_user->ID;
			$current_user = null;
			wp_set_current_user( $cur_id );
			return $current_user;
		}

		// $current_user has a junk value. Force to WP_User with ID 0.
		$current_user = null;
		wp_set_current_user( 0 );
		return $current_user;
	}

	if ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) {
		wp_set_current_user( 0 );
		return $current_user;
	}

	/**
	 * Filters the current user.
	 *
	 * The default filters use this to determine the current user from the
	 * request's cookies, if available.
	 *
	 * Returning a value of false will effectively short-circuit setting
	 * the current user.
	 *
	 * @since 3.9.0
	 *
	 * @param int|false $user_id User ID if one has been determined, false otherwise.
	 */
	$user_id = apply_filters( 'determine_current_user', false );
	if ( ! $user_id ) {
		wp_set_current_user( 0 );
		return $current_user;
	}

	wp_set_current_user( $user_id );

	return $current_user;
}

注意事项

  • 此函数是内部函数,通常通过 wp_get_current_user() 调用,开发者应优先使用后者。
  • 过滤器 determine_current_user 可用于自定义用户身份验证逻辑,例如基于 API 密钥或自定义会话。
  • 在 XMLRPC 请求中,默认将当前用户设置为 ID 0,以确保安全性和一致性。
  • 函数会升级 stdClass 对象到 WP_User 实例,以保持类型一致性。

📄 原文内容

Retrieves the current user object.

Description

Will set the current user, if the current user is not set. The current user will be set to the logged-in person. If no user is logged-in, then it will set the current user to 0, which is invalid and won’t have any permissions.

This function is used by the pluggable functions wp_get_current_user() and get_currentuserinfo() , the latter of which is deprecated but used for backward compatibility.

See also

Return

WP_User Current WP_User instance.

Source

function _wp_get_current_user() {
	global $current_user;

	if ( ! empty( $current_user ) ) {
		if ( $current_user instanceof WP_User ) {
			return $current_user;
		}

		// Upgrade stdClass to WP_User.
		if ( is_object( $current_user ) && isset( $current_user->ID ) ) {
			$cur_id       = $current_user->ID;
			$current_user = null;
			wp_set_current_user( $cur_id );
			return $current_user;
		}

		// $current_user has a junk value. Force to WP_User with ID 0.
		$current_user = null;
		wp_set_current_user( 0 );
		return $current_user;
	}

	if ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) {
		wp_set_current_user( 0 );
		return $current_user;
	}

	/**
	 * Filters the current user.
	 *
	 * The default filters use this to determine the current user from the
	 * request's cookies, if available.
	 *
	 * Returning a value of false will effectively short-circuit setting
	 * the current user.
	 *
	 * @since 3.9.0
	 *
	 * @param int|false $user_id User ID if one has been determined, false otherwise.
	 */
	$user_id = apply_filters( 'determine_current_user', false );
	if ( ! $user_id ) {
		wp_set_current_user( 0 );
		return $current_user;
	}

	wp_set_current_user( $user_id );

	return $current_user;
}

Hooks

apply_filters( ‘determine_current_user’, int|false $user_id )

Filters the current user.

Changelog

Version Description
4.5.0 Introduced.