I’m trying to build a simple wordpress password change script of my own (well, based on a plugin really) – the password is successfully changed – but it logs me out after the change completes! Below is the code used. Can anyone see where I’m being logged out and how to prevent it? Thanks!
$update = $wpdb->query($wpdb->prepare("UPDATE {$wpdb->users} SET `user_pass` = %s WHERE `ID` = %d",array(wp_hash_password($_POST['admin_pass1']),$user_ID)));
if(!is_wp_error($update))
{
wp_cache_delete($user_ID,'users');
wp_cache_delete($user->user_login,'userlogins');
wp_logout();
if (wp_signon(array('user_login'=>$user->user_login,'user_password'=>$_POST['admin_pass1']),false)):
wp_redirect(admin_url());
endif;
ob_start();
}
After resetting password you have to set/reset cookies (http://codex.wordpress.org/Function_Reference/wp_set_auth_cookie)
like this
To reset the password you’d better use wordpress functions like wp_check_password and wp_set_password because of integration with other applications/plugins.
Was linked here from another post, and wanted to give an updated solution to this problem, as some of these solutions (especially modifying wpdb->query directly) aren’t best practice anymore.
Update the user’s password using wp_set_password(), and then log the user back in, using wp_signon().
wp_signon will create the authentication cookie for you, as other users have suggested, but in a much more streamlined way.
Actually this:
means that if there are no errors the following functions will be executed. One of this functions is
wp_logout()
which will be always called if the conditional block is executed.If it’s not what you want, then you want to consider replacing:
with:
Try below code, it won’t log you out after password change and it works with Ajax too. Also, no need to reset cookies/session after it.
Cheers
Make sure the code is run before the end of HTTP headers and the beginning of regular page content. You may not get any warning if you inadvertently wait too long in the page generation process. It’ll all just fail silently and you’ll be logged out mysteriously (even though
wp_signon()
returns a validWP_User
object).if you still look for an answer on this topic:, i found a solution!
in short, after you update the password, clear the data and logout
( as you did)
user is logged out now
then
do a ‘redirect’ to a new page to auto-login again
Catch the call to this page via a
add_action( 'wp', 'auto_login' );
(we must do this, before anything is send via ‘headers’)
the auto_login function then can handle your request to auto login the given user.(via $_GET parameters)
So when i redirect to the new page i pass on two parameters
user_id (the user to login)
a secret key (for security)
then in the auto_login function i look for those two parameters
decrypt the secret key to check if this is oke
if so,
then login the given user
do some more security checks on this,
like user_id must be valid etc
if all oke,
then you can redirect him to a home_page again
hope this helps your issue