reset_password()
Resets (changes) the password of the specified user.
Updates the specified password in the DB, resets the user's cache, and disables the notification about the need to change the password.
Differs from wp_set_password() in that it triggers hooks:
- password_reset - before changing the password.
- after_password_reset after changing the password.
Uses: wp_set_password()
Hooks from the function
Returns
null
. Nothing (void).
Usage
reset_password( $user, $new_pass );
- $user(WP_User) (required)
- The user whose password needs to be changed.
- $new_pass(string) (required)
- The new password for the user in plain text.
Examples
#1 Change the user's password programmatically
An example of code that resets the password for a specific user.
$user = get_user_by( 'ID', 2 ); $new_pass = 'mynewpassword'; reset_password( $user, $new_pass );
#2 Password change via GET parameter
Add the code to functions.php, then open the site with ?change_pass=userlogin
, to set a new password newpassword
for the user.
if ( isset( $_GET['change_pass'] ) ) { add_action( 'init', function () { $user = get_user_by( 'login', $_GET['change_pass'] ); if ( $user ) { reset_password( $user, 'newpassword' ); } } ); }
Changelog
Since 2.5.0 | Introduced. |