wp_set_current_user()WP 2.0.3

Changes the current user by ID or name. The function does not authorize the user but changes the global variable $current_user and its associated variables.

Set $id = null and specify a name if you do not know a user's ID.

Some actions in WordPress require authorization. With this function, you can allow unauthorized users or other roles to perform such actions.

Sets global variables:

$user_ID    = (int) $user->ID;
$user_level = (int) $user->user_level;
$userdata   = $user;
$user_login = $user->user_login;
$user_email = $user->user_email;
$user_url   = $user->user_url;
$user_identity = $user->display_name;

See setup_userdata()

Pluggable function — this function can be replaced from a plugin. It means that this function is defined (works) only after all plugins are loaded (included), but before this moment this function has not defined. Therefore, you cannot call this and all functions depended on this function directly from a plugin code. They need to be called on plugins_loaded hook or later, for example on init hook.

Function replacement (override) — in must-use or regular plugin you can create a function with the same name, then it will replace this function.

Hooks from the function

Return

WP_User. Current user User object.

Usage

wp_set_current_user( $id, $name );
$id(int|null) (required)
User ID. You can specify null if the user ID is unknown.
$name(string)
Username (login).
Default: ''

Examples

0

#1 Reinitialize the current user

Attention! Running this function will affect everything related to the current user (real user)!

Let's say we need to do something for a special user in runtime, but the code we run is depends on $current_user variable and we have no ability to chenge that code. To solve that problem we can switch to that special user do the stuff, and then switch back to original user.

// save ID of the current real user
$original_user_id = get_current_user_id();

// switch to user 15
wp_set_current_user( 15 );

// Do our special stuff here...

// Restore the original user.
wp_set_current_user( $original_user_id );
0

#2 Reinitialize the current user (case 2)

Let's say we update the data in the user profile and after updating the data, we need to reinstall the already installed global variable $current_user and all other global variables associated with the current user.

Ie, we need to another call of wp_get_current_user() function returns the current user data after the update.

global $current_user;
$cuser_id = $current_user->ID;    // save ID
unset( $current_user );           // remove to wp_set_current_user() reinstall everything
wp_set_current_user( $cuser_id ); // reinstall
-1

#3 Set the current user and authorize it

This example shows how to set the current user and authorize it:

$user_id = 123;
$user    = get_user_by( 'id', $user_id );
if( $user ) {
	wp_set_current_user( $user_id, $user->user_login );
	wp_set_auth_cookie( $user_id );
	do_action( 'wp_login', $user->user_login );
}

Notes

  • Global. WP_User. $current_user The current user object which holds the user data.

Changelog

Since 2.0.3 Introduced.

wp_set_current_user() code WP 6.4.3

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