The password_reset WordPress PHP action fires before the user’s password is reset.
Usage
add_action('password_reset', 'your_custom_function', 10, 2);
function your_custom_function($user, $new_pass) {
// Your custom code here
return;
}
Parameters
$user(WP_User): The user object.$new_pass(string): The new user password.
More information
See WordPress Developer Resources: password_reset
Examples
Log password reset events
Log password reset events to a custom log file.
add_action('password_reset', 'log_password_reset', 10, 2);
function log_password_reset($user, $new_pass) {
$log_entry = "Password reset for user: {$user->user_login} ({$user->user_email}) at " . current_time('mysql') . "\n";
file_put_contents(WP_CONTENT_DIR . '/password_reset.log', $log_entry, FILE_APPEND);
}
Send custom email notification on password reset
Send a custom email notification to the user when their password is reset.
add_action('password_reset', 'custom_password_reset_email', 10, 2);
function custom_password_reset_email($user, $new_pass) {
$subject = 'Your password has been reset';
$message = "Hi {$user->display_name},\n\nYour password has been reset. Please use the following new password to log in: {$new_pass}\n\nBest regards,\nThe Team";
wp_mail($user->user_email, $subject, $message);
}
Set custom meta data on password reset
Set a custom user meta value when the user’s password is reset.
add_action('password_reset', 'set_password_reset_meta', 10, 2);
function set_password_reset_meta($user, $new_pass) {
update_user_meta($user->ID, 'last_password_reset', current_time('mysql'));
}
Log password strength
Log the password strength of the new password when it is reset.
add_action('password_reset', 'log_password_strength', 10, 2);
function log_password_strength($user, $new_pass) {
$strength = wp_check_password_strength($new_pass);
$log_entry = "Password strength for {$user->user_login} ({$user->user_email}): {$strength}\n";
file_put_contents(WP_CONTENT_DIR . '/password_strength.log', $log_entry, FILE_APPEND);
}
Notify admin of password reset
Send an email notification to the admin when a user’s password is reset.
add_action('password_reset', 'notify_admin_password_reset', 10, 2);
function notify_admin_password_reset($user, $new_pass) {
$admin_email = get_option('admin_email');
$subject = "User password reset: {$user->user_login}";
$message = "The password for user {$user->user_login} ({$user->user_email}) has been reset.";
wp_mail($admin_email, $subject, $message);
}