If you are a WordPress developer, have you ever faced a situation when you need to do auto login after password reset in WordPress? i faced the situation. Do you know how to do auto login after password reset in WordPress?

Actually before a week i faced the issue, searched over net, but not found satisfactory result. At last i gone through the documentation and found some useful hooks.

In WordPress there are before and after hooks available, like post save hook.

In my case i found some useful hooks

1) password_reset action hook

<?php

add_action( 'password_reset', 'before_password_update', 10, 2 );

function before_password_update( $user, $new_pass ) {
//write code before password reset.
}
?>

This hook runs after the user submits a new password and before the new password is set. Mean you have entered the new password but password not updated in db.
2) after_password_reset hook

<?php

function action_after_password_update( $wp_password_change_notification ) {
//write code after password reset.
};

add_action( 'after_password_reset', 'action_after_password_update', 10, 1 );

?>

This hook runs after the user submits a new password and after the new password is set.

3) If you are using the WooCommerce in this case you have to use the following hook

woocommerce_customer_reset_password

<?php

function action_woocommerce_reset_password( $user ) {
$login=$_POST['reset_login'];
$pass=$_POST['password_1'];
$creds = array(
'user_login' => $login,
'user_password' => $pass,
'remember' => true
);

wp_signon( $creds, false );
};
add_action( 'woocommerce_customer_reset_password', 'action_woocommerce_reset_password', 10, 1 );

?>

WooCommerce overrides the core WordPress login so, if you are using WooCommerce you have use the exact above code for auto login after password reset. In action_woocommerce_reset_password function wp_signon core function isused for login. if we pass the required parameter as written in the code, wp_signon function will do login in wordpress.
The above piece of code will do all the thing as soon as you entered the new password.

Leave comment

Your email address will not be published. Required fields are marked with *.