Title: wp_set_current_user
Published: April 25, 2014
Last modified: August 20, 2026

---

# wp_set_current_user( int|null $id, string $name = '' ): 󠀁[WP_User](https://developer.wordpress.org/reference/classes/wp_user/)󠁿

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/wp_set_current_user/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/wp_set_current_user/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/wp_set_current_user/?output_format=md#return)
 * [More Information](https://developer.wordpress.org/reference/functions/wp_set_current_user/?output_format=md#more-information)
 * [Source](https://developer.wordpress.org/reference/functions/wp_set_current_user/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/functions/wp_set_current_user/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/functions/wp_set_current_user/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_set_current_user/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/wp_set_current_user/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/wp_set_current_user/?output_format=md#wp--skip-link--target)

Changes the current user by ID or name.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/wp_set_current_user/?output_format=md#description)󠁿

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.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/wp_set_current_user/?output_format=md#parameters)󠁿

 `$id`int|nullrequired

User ID.

`$name`stringoptional

User’s username.

Default:`''`

## 󠀁[Return](https://developer.wordpress.org/reference/functions/wp_set_current_user/?output_format=md#return)󠁿

 [WP_User](https://developer.wordpress.org/reference/classes/wp_user/) Current user
User object.

## 󠀁[More Information](https://developer.wordpress.org/reference/functions/wp_set_current_user/?output_format=md#more-information)󠁿

This function can be replaced via plugins. If plugins do not redefine these functions,
then this will be used instead.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/wp_set_current_user/?output_format=md#source)󠁿

    ```php
    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 )
    	&& ( (int) $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;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/pluggable.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/pluggable.php#L27)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/pluggable.php#L27-L51)

## 󠀁[Hooks](https://developer.wordpress.org/reference/functions/wp_set_current_user/?output_format=md#hooks)󠁿

 [do_action( ‘set_current_user’ )](https://developer.wordpress.org/reference/hooks/set_current_user/)

Fires after the current user is set.

## 󠀁[Related](https://developer.wordpress.org/reference/functions/wp_set_current_user/?output_format=md#related)󠁿

| Uses | Description | 
| [WP_User::__construct()](https://developer.wordpress.org/reference/classes/wp_user/__construct/)`wp-includes/class-wp-user.php` |

Constructor.

  | 
| [setup_userdata()](https://developer.wordpress.org/reference/functions/setup_userdata/)`wp-includes/user.php` |

Sets up global user vars.

  | 
| [do_action()](https://developer.wordpress.org/reference/functions/do_action/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to an action hook.

  |

[Show 1 more](https://developer.wordpress.org/reference/functions/wp_set_current_user/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/wp_set_current_user/?output_format=md#)

| Used by | Description | 
| [wp_send_note_notification()](https://developer.wordpress.org/reference/functions/wp_send_note_notification/)`wp-includes/comment.php` |

Sends a single note mention notification email.

  | 
| [WP_Customize_Manager::_publish_changeset_values()](https://developer.wordpress.org/reference/classes/wp_customize_manager/_publish_changeset_values/)`wp-includes/class-wp-customize-manager.php` |

Publishes the values of a changeset.

  | 
| [_wp_get_current_user()](https://developer.wordpress.org/reference/functions/_wp_get_current_user/)`wp-includes/user.php` |

Retrieves the current user object.

  | 
| [rest_cookie_check_errors()](https://developer.wordpress.org/reference/functions/rest_cookie_check_errors/)`wp-includes/rest-api.php` |

Checks for errors when using cookie-based authentication.

  | 
| [WP_Importer::set_user()](https://developer.wordpress.org/reference/classes/wp_importer/set_user/)`wp-admin/includes/class-wp-importer.php` |  | 
| [wp_logout()](https://developer.wordpress.org/reference/functions/wp_logout/)`wp-includes/pluggable.php` |

Logs the current user out.

  | 
| [set_current_user()](https://developer.wordpress.org/reference/functions/set_current_user/)`wp-includes/pluggable-deprecated.php` |

Changes the current user by ID or name.

  | 
| [wp_xmlrpc_server::login()](https://developer.wordpress.org/reference/classes/wp_xmlrpc_server/login/)`wp-includes/class-wp-xmlrpc-server.php` |

Logs user in.

  |

[Show 3 more](https://developer.wordpress.org/reference/functions/wp_set_current_user/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/wp_set_current_user/?output_format=md#)

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/wp_set_current_user/?output_format=md#changelog)󠁿

| Version | Description | 
| [2.0.3](https://developer.wordpress.org/reference/since/2.0.3/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/wp_set_current_user/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 3 content](https://developer.wordpress.org/reference/functions/wp_set_current_user/?output_format=md#comment-content-772)
 2.    [Codex](https://profiles.wordpress.org/codex/)  [  11 years ago  ](https://developer.wordpress.org/reference/functions/wp_set_current_user/#comment-772)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_set_current_user%2F%23comment-772)
     Vote results for this note: 3[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_set_current_user%2F%23comment-772)
 4.  Note that setting the current user does not log in that user. This example will
     set the current user and log them in.
 5.      ```php
         $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, $user );
         }
         ```
     
 6.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_set_current_user%2F%3Freplytocom%3D772%23feedback-editor-772)
 7.   [Skip to note 4 content](https://developer.wordpress.org/reference/functions/wp_set_current_user/?output_format=md#comment-content-2629)
 8.    [Andrei Surdu](https://profiles.wordpress.org/_smartik_/)  [  9 years ago  ](https://developer.wordpress.org/reference/functions/wp_set_current_user/#comment-2629)
 9.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_set_current_user%2F%23comment-2629)
     Vote results for this note: 2[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_set_current_user%2F%23comment-2629)
 10. **Attention!**
      Running this function will affect everything related to the current(
     real) user, after it. You must restore the real user:
 11.     ```php
         $original_user_id = get_current_user_id(); // For example the real user ID is 7
         wp_set_current_user( 15 );
     
         // get_current_user_id() now returns 15
         // Do other stuff here...
         // ...
         // get_current_user_id() still returns 15
     
         // Restore the original user.
         wp_set_current_user( $original_user_id );
     
         // get_current_user_id() now returns 7
         ```
     
 12.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_set_current_user%2F%3Freplytocom%3D2629%23feedback-editor-2629)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_set_current_user%2F)
before being able to contribute a note or feedback.