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


5 Answers 5


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.
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).


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


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

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.

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.';


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 );

      //do something with an error, if there is one.
      //do something with the successful change. 
  • Still signs out.
    – Kirkland
    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.