5

I need change WP password without logout profile. When I use wp_set_password( $password, $user_ID ) i have problem with logout.

1

5 Answers 5

9

If you are changing the password for the current logged-in user, it will log you out. You have to log in again:

// Get current logged-in user.
$user = wp_get_current_user();

// Change password.
wp_set_password($new_password, $user->ID);

// Log-in again.
wp_set_auth_cookie($user->ID);
wp_set_current_user($user->ID);
do_action('wp_login', $user->user_login, $user);

Note that since you are setting a new log-in cookie (i.e. changing headers), you need to run this code before any other output (HTML or echos).

7

Try below code, it won't log you out after the password change and it works with Ajax functions too. No need to reset cookies/sessions afterwards.

$userdata['ID'] = 1; //user ID
$userdata['user_pass'] = 'new_password';
wp_update_user( $userdata ); // this will handle encryption and everything

Cheers

5
  • you sure? have you actually tried it? To be specific, after changing the password WP will most likely required login Commented Feb 11, 2016 at 10:23
  • 1
    yes I did tried this... it works perfectly Commented Feb 11, 2016 at 11:03
  • Triggers "cannot modify headers" and logs the user out next time they navigate.
    – Kirkland
    Commented Feb 12, 2020 at 10:32
  • I have just tested this and it is working fine for me, please post your code here. Commented Feb 14, 2020 at 2:02
  • Excellent solution, it works with admin-ajax
    – Jana
    Commented Dec 18, 2020 at 12:38
1

I have no working example, only hints for thinking about this topic. The function to set a new password via the function of the core is right. But if you are using this function on the user update page, then the user will be automatic get logged out as it is deleting the cache of the logged-in users.

After the logout, can you hook in wp and create a new login. You must do this before anything is sent via 'headers'.

`add_action( 'wp', 'your_login' );`

Also think about the cookie of the login - wp_set_auth_cookie! Also you can delete the current cache for the user - wp_cache_delete().

Maybe this question and his answers help you, the same topic.

0
function change_client_password() {
    $current_user     = $_POST['current_user'];
    $current_password = $_POST['current_password'];
    $new_password     = $_POST['new_password'];
    $confirm_password = $_POST['confirm_password'];
    $user             = get_user_by( 'ID', $current_user );

    $result = wp_check_password( $current_password, $user->data->user_pass, $current_user );

    if ( $result ) {
        if ( $new_password == $confirm_password ) {
            wp_set_password( $new_password, $current_user );
            wp_set_auth_cookie ( $current_user );
            wp_set_current_user( $current_user );
            do_action('wp_login', $user->user_login, $user );
            echo 'Your new password is changed.';
        } else {
            echo 'Your current password is correct, but the new and confirm passwords do not match.';
        }
    } else {
        echo 'Your current password is incorrect.';
    }

    wp_die();
}
0

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.

function create_new_password_for_user($new_password){
    //Get the current user's details, while they're still signed in, in this scope.
     $current_user = wp_get_current_user();
     $current_user_id = $current_user->ID;
     $users_login = $current_user->user_email;

    //set their new password (this will trigger the logout)
    wp_set_password($new_password, $current_user_id);

    //setup the data to be passed on to wp_signon
    $user_data = array(
            'user_login'     => $users_login,
            'user_password'    => $new_password,
            'remember'        => false
        );

    // Sign them back in.
    $result = wp_signon( $user_data );

    if(is_wp_error($result)){
      //do something with an error, if there is one.
    }else{
      //do something with the successful change. 
    }
}
1
  • Still signs out.
    – Kirkland
    Commented Feb 12, 2020 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.