wp_set_current_user

函式
wp_set_current_user ( $id, $name = '' )
引數
  • (int|null) $id User ID.
    Required:
  • (string) $name User's username.
    Required:
    Default: (empty)
返回值
  • (WP_User) Current user User object.
定義位置
相關方法
wp_get_current_user_wp_get_current_userset_current_userget_current_user_idset_current_screen
引入
2.0.3
棄用
-

wp_set_current_user: 這是一個WordPress的函式,用於設定當前使用者。它允許你將當前使用者設定為一個特定的使用者ID,這可以用來作為該使用者進行操作,如更新他們的個人資料或代表他們建立一個文章。

通過ID或名字改變當前使用者。

如果你不知道一個使用者的ID,可以將$id設定為null並指定一個名字。

一些WordPress的功能是基於當前使用者的,而不是基於登入使用者的。因此,它開啟了對未登入的使用者進行編輯和執行操作的許可權。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function wp_set_current_user( $id, $name = '' ) {
global $current_user;
// If `$id` matches the current user, there is nothing to do.
if ( isset( $current_user )
&& ( $current_user instanceof WP_User )
&& ( $id == $current_user->ID )
&& ( null !== $id )
) {
return $current_user;
}
$current_user = new WP_User( $id, $name );
setup_userdata( $current_user->ID );
/**
* Fires after the current user is set.
*
* @since 2.0.1
*/
do_action( 'set_current_user' );
return $current_user;
}
endif;
if ( ! function_exists( 'wp_get_current_user' ) ) :
/**
* Retrieves the current user object.
*
* 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.
*
* @since 2.0.3
*
* @see _wp_get_current_user()
* @global WP_User $current_user Checks if the current user is set.
*
* @return WP_User Current WP_User instance.
*/
function wp_set_current_user( $id, $name = '' ) { global $current_user; // If `$id` matches the current user, there is nothing to do. if ( isset( $current_user ) && ( $current_user instanceof WP_User ) && ( $id == $current_user->ID ) && ( null !== $id ) ) { return $current_user; } $current_user = new WP_User( $id, $name ); setup_userdata( $current_user->ID ); /** * Fires after the current user is set. * * @since 2.0.1 */ do_action( 'set_current_user' ); return $current_user; } endif; if ( ! function_exists( 'wp_get_current_user' ) ) : /** * Retrieves the current user object. * * 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. * * @since 2.0.3 * * @see _wp_get_current_user() * @global WP_User $current_user Checks if the current user is set. * * @return WP_User Current WP_User instance. */
function wp_set_current_user( $id, $name = '' ) {
		global $current_user;

		// If `$id` matches the current user, there is nothing to do.
		if ( isset( $current_user )
		&& ( $current_user instanceof WP_User )
		&& ( $id == $current_user->ID )
		&& ( null !== $id )
		) {
			return $current_user;
		}

		$current_user = new WP_User( $id, $name );

		setup_userdata( $current_user->ID );

		/**
		 * Fires after the current user is set.
		 *
		 * @since 2.0.1
		 */
		do_action( 'set_current_user' );

		return $current_user;
	}
endif;

if ( ! function_exists( 'wp_get_current_user' ) ) :
	/**
	 * Retrieves the current user object.
	 *
	 * 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.
	 *
	 * @since 2.0.3
	 *
	 * @see _wp_get_current_user()
	 * @global WP_User $current_user Checks if the current user is set.
	 *
	 * @return WP_User Current WP_User instance.
	 */

常見問題

FAQs
檢視更多 >