0

I am trying to redirect a default WP login form, if it's HTTP referer contains some specific query string parameter.

Scenario: I need to redirect new users (the ones who just created their passwords using the link in their e-mail) to my custom login form, once the password is generated.

The steps in WP password creation go like this:

  1. /wp-login.php?action=rp (this is the link you get in your e-mail, you type your new password there)
  2. /wp-login.php?action=resetpass (this is the screen where it says your password was saved and you can login)
  3. /wp-login.php (once the password is created, there is no query parameter anymore)

I am able to redirect it when the action=resetpass argument is present, but that way the password won't get generated and my custom form thows "wrong password" error.

My thinking is: I should be able to redirect the wp-login.php page if it's HTTP referer contained action=resetpass argument, that way it would fit my needs just fine. Here's my code:

<?php
add_action('init', 'prevent_wp_login');

function prevent_wp_login() {
    global $pagenow;
    $referer = wp_get_referer();

    if( ($pagenow == 'wp-login.php' && strpos($referer, 'action=resetpass') !== false)) {

        $page = site_url('/my-custom-form');
        wp_redirect($page);
        exit();
    }
}
?>

But I am getting "too many redirects". What's the problem?

PS: I've also tried wp_get_original_referer() function, and getting the referer like this:

parse_str(parse_url($_SERVER['HTTP_REFERER'], PHP_URL_QUERY), $queries);
$referer= $queries['q'];

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.