WordPress at a glance

wp_set_current_user() WP 1.0

Changes the current user by ID or name.

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.

  • 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()
Hooks from function:

WP_User. Current user User object


wp_set_current_user( $id, $name );
$id(int) (required)
User ID
User's username
Default: ''

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

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