3

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

4

You can try below code. It won't log you out after changing password.

$userdata['ID'] = 1; //user ID
$userdata['user_pass'] = 'new_password';
wp_update_user( $userdata );

Enjoy ;)

  • you sure? have you actually tried it? To be specific, after changing the password WP will most likely required login – Mark Kaplun Feb 11 '16 at 10:23
  • yes I did tried this... it works perfectly – Mayur Chauhan Feb 11 '16 at 11:03
3

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

0

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

After the logout, can you hook in wp and create a new login. You must do this, before anything is send 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 helps you, 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();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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