This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.
password_reset action
Fires before the user’s password is reset.
To use password_reset action, first you have to register it using add_action. You can write this code into functions.php of your activated theme or in a custom WordPress Plugin.
We at WePlugins, always prefer to create a custom WordPress Plugin while using hooks so nothing breaks when you update your WordPress Theme in the future.
In the below live example, we have defined a function execute_on_password_reset_event which takes 2 parameters and we registered using add_action. The first parameter password_reset is the name of the hook, The second parameter execute_on_password_reset_event is the name of the function which needs to be called, third parameter is the priority of calling the hook if the same hook is used multiple times and the last parameter is the number of arguments (if any) to be passed in the registered function.
Sometimes, you have to remove a registered hook so you can use remove_action to remove password_reset action.
Parameters
- $user : (WP_User) The user.
- $new_pass : (string) New user password.
Below are the 2 parameters required to use this hook.
Live Example
do_action( 'password_reset', WP_User $user, string $new_pass )
Below is an example how you can use this hook.
function weplugins_execute_on_password_reset_event($user, $new_pass){ //You can write code here to be executed when this action occurs in WordPress. Use the parameters received in the function arguments & implement the required additional custom functionality according to your website requirements. } // add the action add_action( "password_reset", "weplugins_execute_on_password_reset_event" , 10, 2);
To remove a hook callback, use the example below.
remove_action( "password_reset", "weplugins_execute_on_password_reset_event", 10, 2 );
Please make sure to provide the same callback function name, priority and number of arguments while removing the hook callback.
Example 1: Send a Custom Email on Password Reset
In this example, we’ll send an email to the user when their password is reset.
function weplugins_send_custom_email_on_password_reset($user, $new_pass) { $email = $user->user_email; $subject = "Your Password Has Been Reset"; $message = "Hello " . $user->display_name . ",\n\nYour new password is: " . $new_pass; wp_mail($email, $subject, $message); } add_action("password_reset", "weplugins_send_custom_email_on_password_reset", 10, 2);
Example 2: Log Password Resets
This example logs the password reset events in a custom table.
function weplugins_log_password_reset($user, $new_pass) { global $wpdb; $wpdb->insert( $wpdb->prefix . 'password_reset_log', array( 'user_id' => $user->ID, 'reset_time' => current_time('mysql'), 'new_password' => $new_pass ) ); } add_action("password_reset", "weplugins_log_password_reset", 10, 2);
Example 3: Update User Meta on Password Reset
Here, we update a custom user meta field whenever the password is reset.
function weplugins_update_user_meta_on_password_reset($user, $new_pass) { update_user_meta($user->ID, 'last_password_reset', current_time('mysql')); } add_action("password_reset", "weplugins_update_user_meta_on_password_reset", 10, 2);
Contact Us
If you need any customization, feel free to contact us.
Explore the latest in WordPress
Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.