I'm working on a custom WordPress login module and I faced some problem. If a user finishes his survey on the site he will be registered with his email address and then password reset link will be sent to the user's email box. If he click the link he will be redirected to reset password panel. Once he set password he is redirected to login panel, but I want to make him automatically logged in and redirected to a specific page. So the idea is to remove unnecessary login step. Is it possible? Thanks
0
votes
Welcome to stack ChengMin, it's a good idea on stack to research and try a few things before asking a question. Then you can say what you have found, what you have tried. You'll learn a lot in the process, and get more targeted help. Often it has been asked before. For example: it looks like this may help. Similar, except they doing if after registration. stackoverflow.com/questions/19949876/…
- anmari
Thanks for your answer. But before post this question, I checked all articles here and there (including the article you mentioned above), But couldn't find the correct answer. I looked in WordPress core and found the solution by myself. :)
- Intelligent Web Solutions
1 Answers
8
votes
I finally managed it by myself with below code. Hope this code helps the others!
add_action( 'validate_password_reset', 'rsm_redirect_after_rest', 10, 2 );
function rsm_redirect_after_rest($errors, $user) {
if ( ( ! $errors->get_error_code() ) && isset( $_POST['pass1'] ) && !empty( $_POST['pass1'] ) ) {
reset_password( $user, $_POST['pass1'] );
list( $rp_path ) = explode( '?', wp_unslash( $_SERVER['REQUEST_URI'] ) );
$rp_cookie = 'wp-resetpass-' . COOKIEHASH;
setcookie( $rp_cookie, ' ', time() - YEAR_IN_SECONDS, $rp_path, COOKIE_DOMAIN, is_ssl(), true );
wp_set_current_user( $user->ID );
wp_set_auth_cookie( $user->ID );
do_action( 'wp_login', $user->user_login, $user );//`[Codex Ref.][1]
wp_redirect( home_url() );
exit;
}
}