检索当前用户对象。

原型

_wp_get_current_user()

描述

如果未设置当前用户,将设置当前用户。当前用户将设置为登录人员。如果没有用户登录,则会将当前用户设置为0,这是无效的,并且没有任何权限。

参考:

  • wp_get_current_user()

返回值

(WP_User)  当前

源文件

路径:wp-includes/user.php

<?php
...
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|bool $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;
}
...
?>

其他

英文文档:https://developer.wordpress.org/reference/functions/_wp_get_current_user/