简体   繁体   中英

WordPress logout after password change “something went wrong” message

I use a form and the wp_update_user function to let users change their password in the WordPress frontend. For this i use an ajax function which executes wp_update_user:

    /* CHANGE PASSWORD OF USER */
    if ( !empty($pass1 ) && !empty( $pass2 ) ) {
        if ( $pass1 == $pass2 ) {
            wp_update_user( array( 'ID' => $userID, 'user_pass' => esc_attr( $pass2 ) ) );
            echo "<div class='message-hub-success'>Passsword changed!</div>";
        } else {
             echo "<div class='message-hub-error'>The passwords you entered do not match.  Your password was not updated.</div>"; 
        }
    }

The password change is working as expected. But when the user clicks the logout-link after the password change i get the error "something went wrong" instead of a the logout and the redirect function. My logout link:

<a href="<?php echo wp_logout_url( home_url() ); ?>">Logout</a>

The something went wrong message is not shown on the page but on a wordpress page with a logout link: 在此处输入图片说明

The reason is that when you update the password, the user's cookies are cleared (see the documentation for wp_update_user ).

Then, during logout, the user's session is not available, which causes the issue you are experiencing.

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