wp_set_current_user

The timeline below displays how wordpress function wp_set_current_user has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 5.9

/**
 * 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.
 *
 * @since 2.0.3
 *
 * @global WP_User $current_user The current user object which holds the user data.
 *
 * @param int|null $id   User ID.
 * @param string   $name User's username.
 * @return WP_User Current user User object.
 */
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;
}

WordPress Version: 5.5

/**
 * 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.
 *
 * @since 2.0.3
 *
 * @global WP_User $current_user The current user object which holds the user data.
 *
 * @param int    $id   User ID
 * @param string $name User's username
 * @return WP_User Current user User object
 */
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;
}

WordPress Version: 5.2

/**
 * 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.
 *
 * @since 2.0.3
 * @global WP_User $current_user The current user object which holds the user data.
 *
 * @param int    $id   User ID
 * @param string $name User's username
 * @return WP_User Current user User object
 */
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;
}

WordPress Version: 4.4

/**
 * 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.
 *
 * @since 2.0.3
 * @global WP_User $current_user The current user object which holds the user data.
 *
 * @param int    $id   User ID
 * @param string $name User's username
 * @return WP_User Current user User object
 */
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;
}

WordPress Version: 4.3

/**
 * 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.
 *
 * @since 2.0.3
 * @global WP_User $current_user The current user object which holds the user data.
 *
 * @param int    $id   User ID
 * @param string $name User's username
 * @return WP_User Current user User object
 */
function wp_set_current_user($id, $name = '')
{
    global $current_user;
    if (isset($current_user) && $current_user instanceof WP_User && $id == $current_user->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;
}

WordPress Version: 3.9

/**
 * 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.
 *
 * @since 2.0.3
 * @global object $current_user The current user object which holds the user data.
 *
 * @param int $id User ID
 * @param string $name User's username
 * @return WP_User Current user User object
 */
function wp_set_current_user($id, $name = '')
{
    global $current_user;
    if (isset($current_user) && $current_user instanceof WP_User && $id == $current_user->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;
}

WordPress Version: 3.7

/**
 * 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.
 *
 * @since 2.0.3
 * @global object $current_user The current user object which holds the user data.
 * @uses do_action() Calls 'set_current_user' hook after setting the current user.
 *
 * @param int $id User ID
 * @param string $name User's username
 * @return WP_User Current user User object
 */
function wp_set_current_user($id, $name = '')
{
    global $current_user;
    if (isset($current_user) && $current_user instanceof WP_User && $id == $current_user->ID) {
        return $current_user;
    }
    $current_user = new WP_User($id, $name);
    setup_userdata($current_user->ID);
    do_action('set_current_user');
    return $current_user;
}