How to auto login user again after change user_login

I found when attempting similar I needed to clear the user cache to get the relogin to work (after much frustrating testing!):

wp_cache_delete($user_id, 'users');
wp_cache_delete($old_user_login, 'userlogins'); // maybe unnecessary?
$creds = array('user_login' => $user_login, 'user_password' => $user_password, 'remember' => true);
wp_signon($creds);

Note for this to work you may also need the user to change their password at the same time so that you can populate the $user_password field with a plain text password to provide to wp_signon

Leave a Comment