8

I would like to able to redirect to get_bloginfo('url'); after reseting password.

But I cannot for life of me find any simple answer or function to do this.

Does anyone know if this is possible?

Thank Josh

7

Here is a simple solution. Im hooking into login_headerurl. Maybe there is a better hook for this but it works, Put this in your functions.php:

function wpse_lost_password_redirect() {

    // Check if have submitted 
    $confirm = ( isset($_GET['checkemail'] ) ? $_GET['checkemail'] : '' );

    if( $confirm ) {
        wp_redirect( home_url() ); 
        exit;
    }
}
add_action('login_headerurl', 'wpse_lost_password_redirect');

What it does, it runs on login_headerurl and checks for the GET parameter "checkedmail" which you get after you submitted a valid username or email. Then i redirect by using the awsome function wp_redirect to the home_url.

UPDATE after comment

If you want to redirect the user after submitted a new password you only need to use the hook password_reset here is an example:

function wpse_lost_password_redirect() {
    wp_redirect( home_url() ); 
    exit;
}
add_action('after_password_reset', 'wpse_lost_password_redirect');
  • Hi Pontus, thank you very much for your answer, this will come use for another time i'm sure. But I'm in fact after the re-direct after the new passord itself has been reset, not after the email/user submission. See this screenshot of form that I need it the redirect to work on... i.imgur.com/x2IUQ0c.jpg - Thanks – Joshc Jan 26 '13 at 15:39
  • Aaah i see, thats actually easier than the function before. See my updated answer. – Pontus Abrahamsson Jan 26 '13 at 18:51
  • Awgh dude, these are both awesome!! Thanks for sharing with us, it worked at treat! Nice one. – Joshc Jan 27 '13 at 12:32
  • 2
    Beware that using password_reset action is performed before actual password resetting is done. Documentation says Fires before the user's password is reset. – Ejaz Nov 11 '15 at 19:50
  • 2
    The second version does not work correctly because the password will not actually be changed. You must remove the exit because this action is performed before the password is reset. – Jon Surrell Jul 11 '16 at 6:50
9

The "correct" answer doesn't work here because the action 'password_reset' fires before the password is reset.

I modified the first answer, before the update, to work.

function wpse_lost_password_redirect() {

    // Check if have submitted
    $confirm = ( isset($_GET['action'] ) && $_GET['action'] == resetpass );

    if( $confirm ) {
        wp_redirect( home_url() );
        exit;
    }
}
add_action('login_headerurl', 'wpse_lost_password_redirect');

Edit: Didn't have enough rep to comment so I'm posting this as a new answer.

  • This should be posted as an answer. Good job. – Christine Cooper Nov 4 '14 at 15:52
  • Great solution - working on MS also. – Q Studio Apr 28 '18 at 9:51
2

I can't see how the "UPDATE after comment" answer works.

The documentation for the 'password_reset' hook says "Fires before the user's password is reset.".
If you redirect, then exit the password won't get changed.

Since I had a similar need I developed my solution to the problem. We still respond to the "password_reset" hook but instead of performing the redirect immediately we add a hook for the "login_url" filter. And it's in this filter that we add the redirections to the home page after the user has logged in.

 add_action( "password_reset", "rngs_password_reset", 10, 2 );

/**
 * Implement "password_reset" for RNGS
 *
 * After a password reset has been performed we want the Log in link to redirect the user to the home url.
 * When we see this action being run we know that we should be filtering "login_url" to add the redirect the home page.
 * We don't filter "login_url" any other time. 
 *
 * @param WP_User $user - the user object
 * @param string $new_pass - the new password
 *  
 */
function rngs_password_reset( $user, $new_pass ) {
  add_filter( "login_url", "rngs_login_url", 10, 2 );
}

/**
 *  Implement "login_url" filter for RNGS
 *
 * Redirect the user to the home page after logging in
 *
 * @TODO - make this an option field that controls where the logged in user goes
 * @TODO - dependent upon role?
 * 
 * @param string $login_url - the original login_url which is not expected to include "redirect_to" or "reauth"
 * @param string $redirect - expected to be null/blank
 */
function rngs_login_url( $login_url, $redirect ) {
  $home_redirect = home_url();
  $login_url = add_query_arg('redirect_to', urlencode( $home_redirect ), $login_url);
  return( $login_url );
} 
1

Perhaps I'm missing something in the question, but is anything wrong with using the lostpassword_redirect filter?

add_filter( 'lostpassword_redirect', 'my_redirect_home' );

function my_redirect_home( $lostpassword_redirect ) {
    return home_url();
}

More here: https://codex.wordpress.org/Plugin_API/Filter_Reference/lostpassword_redirect

  • What you are missing here, lostpassword_redirect is the filter for a URL where the user will go after he hits Forgot Password button. Then he'll actually reset his password, and when the new password is set, the OP needs a filter/action hook (which is probably after_password_reset) – dev_masta Nov 29 at 18:54
-1

Rediret to is a wordpress feature you don't need to create a plugin for it, just add &redirect_to= the url and it will work. Example:

echo '<a href="'home_url().'/wp-login.php?action=lostpassword&amp;redirect_to='.get_bloginfo('url').'?sli=lost" rel="nofollow" title="Forgot Password">'Forgot Password'</a>';
  • 2
    Answers should be more than a code snippet. Please file an edit and explain why this would work. – Gabriel Aug 15 '15 at 15:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.