简体   繁体   中英

Wordpress Update User's Password with PHP

I have a webapp that uses Wordpress for it's authentication. I have an Account options page there. When opened, it contains a password update section. I POST this to my PHP script and run this code:

wp_update_user(array('ID' => getUserIDWP(), 'user_pass' => $_POST['newpass']))

It logs me out of my current Wordpress session, but when I try to log back in with the password I specified there, it says that I entered an incorrect password. I'd appreciate if someone could shed some light on this subject.

Note: the getUserIDWP() function is an alias for $current_user->ID; and the other related stuff.

WordPress has provided a simple function in new release of WP as below.

wp_set_password( $new_password, $user_id );

OR you can use below code, it will not logout user:

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

https://stackoverflow.com/a/35336444/4819200

It worked for me to update 'user_pass' using both update_user_meta and wp_update_user :

update_user_meta($user_id, 'user_pass', $newpassword);
wp_update_user( array ('ID' => $user_id, 'user_pass' => $newpassword) ) ;

try this one

    global $current_user;
    $password = 'Abc123456';
    wp_set_password($password, $current_user->ID);

I know it's late but...

I had a similar problem and it turns out there was an entry in the usermeta table called user_pass. I deleted that entry and I could log in again.

Maybe this will help somebody - I spent the last hour trying to figure out what I did wrong.

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