WordPress at a glance

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

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

This is a pluggable function, and it can be replaced from plugin. It means that this function is defined (works) only after all plugins are connected (included), but before this moment the function has not yet been defined... Therefore, you cannot call this and all functions depended on this function directly from a plugin code. It must be called via hook plugins_loaded or later, for example on hook init.

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

Is the basis for: wp_get_current_user()
Works based on: setup_userdata()

No Hooks.


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

Code of wp_set_current_user: wp-includes/pluggable.php VER 5.0.1

function wp_set_current_user($id, $name = '') {
	global $current_user;

	// If `$id` matches the user who's already current, there's 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;

Related Functions

From tag: Users (_user)

More from category: Other

No comments
    Hello, !     Log In . Register