WordPress at Your Fingertips

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 to null and specify a name if you do not know a user's ID.

Some WordPress functionality is based on the current user and not based on the signed in user. Therefore, it opens the ability to edit and perform actions on users who aren't signed in.

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()

This is a pluggable function, and it can be replaced by 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 a plugin you can create a function with the same name, then it replace this function.

Hooks from the function


WP_User. Current user User object


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


#1 Set the current user and authorize it

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

$user_id = 12345;
$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 );

#2 Reinstalling the current user

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


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


Since 2.0.3 Introduced.

Code of wp_set_current_user() WP 5.8.1

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;

Users (_user)


vladlu 100vlad.lu
Editors: Kama 100
No comments
    Log In