reset_password()WP 2.5.0

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:

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

0

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

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

reset_password() code WP 6.8.1

function reset_password(
	$user,
	#[\SensitiveParameter]
	$new_pass
) {
	/**
	 * Fires before the user's password is reset.
	 *
	 * @since 1.5.0
	 *
	 * @param WP_User $user     The user.
	 * @param string  $new_pass New user password.
	 */
	do_action( 'password_reset', $user, $new_pass );

	wp_set_password( $new_pass, $user->ID );
	update_user_meta( $user->ID, 'default_password_nag', false );

	/**
	 * Fires after the user's password is reset.
	 *
	 * @since 4.4.0
	 *
	 * @param WP_User $user     The user.
	 * @param string  $new_pass New user password.
	 */
	do_action( 'after_password_reset', $user, $new_pass );
}