简体   繁体   中英

Wordpress when password updated user is logout

I have change password at front-end and im using wp_user_update function,but when user change password it have been log out. the problem is that my old cookies is not updated,so how to update password without log out.have any idea?..

global $wpdb, $current_user;

$user_id =  $current_user->ID;
wp_update_user(array('ID'=>$user_id,'user_pass'=>$_POST['user_pass']));

The answer by Aaron Forgue at the WordPress Support is 3 years old, but might be interesting. I had to change the $wpdb->query() to make it work:

global $wpdb;

$profile_id = $_POST['prof_id'];
$username = $_POST['log_name'];
$password = $_POST['wachtwoord'];
$md5password = wp_hash_password($password);

// You may want to use $wpdb->prepare() here. As it stands, malicous code could be passed in via $_POST['prof_id'] or $_POST['log_name']
$wpdb->query( $wpdb->prepare( 
        "
            UPDATE $wpdb->users SET user_pass = %s WHERE ID = %d
        ", 
        $md5password, 
        $profile_id 
    ) );

// Here is the magic:
wp_cache_delete($profile_id, 'users');
wp_cache_delete($username, 'userlogins'); // This might be an issue for how you are doing it. Presumably you'd need to run this for the ORIGINAL user login name, not the new one.
wp_logout();
wp_signon(array('user_login' => $username, 'user_password' => $password));

Credits go to this plugin for the above trick: http://wordpress.org/extend/plugins/change-password-e-mail/

As mentioned by Robahas , make sure that this code is run before headers are sent, else the wp_signon() will not work and the user will be logged out anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM