wpseek.com
A WordPress-centric search engine for devs and theme authors



wp_set_current_user › WordPress Function

Since2.0.3
Deprecatedn/a
wp_set_current_user ( $id, $name = '' )
Parameters: (2)
  • (int|null) $id User ID.
    Required: Yes
  • (string) $name User's username.
    Required: No
    Default: (empty)
Returns:
  • (WP_User) Current user User object.
Defined at:
Codex:

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.


Source

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

if ( ! function_exists( 'wp_get_current_user' ) ) :
	/**
	 * Retrieves the current user object.
	 *
	 * Will set the current user, if the current user is not set. The current user
	 * will be set to the logged-in person. If no user is logged-in, then it will
	 * set the current user to 0, which is invalid and won't have any permissions.
	 *
	 * @since 2.0.3
	 *
	 * @see _wp_get_current_user()
	 * @global WP_User $current_user Checks if the current user is set.
	 *
	 * @return WP_User Current WP_User instance.
	 */