0

I'm working on a test site, and I was trying to make my site private by redirecting all non logged-in users to the login page by adding the following code to my theme's function.php file:

function redirect_user() {
  if ( ! is_user_logged_in() && ! is_page(array('login','register','lostpassword','contact-us' ))) {
    $return_url = esc_url( home_url( '/login/' ) );
    wp_redirect( $return_url );
    exit;
  }

}
add_action( 'template_redirect', 'redirect_user' );

After some testing, I've encountered an issue while I was trying to reset the password of my test user. After I click the link to reset my password, I'm being redirected to the login page. I'm trying to figure out how I could resolve this.

Since the reset password link is a dynamically generated link, it's not something I could add to my list of pages as shown in the code extract above. I was checking the wordpress documentation and I'm not seeing anything helpful. Any help would be greatly appreciated.

1
  • There is a function called wp_lostpassword_url - you could try comparing to its value and see if that lets them through. – WebElaine Apr 25 '17 at 15:14
0

After reviewing a reset password link, I realised that two variables were being passed via $_GET: key and login. I've added the below code to the beginning of my redirect_user() function:

if(isset($_GET['key'])  && isset($_GET['login']))
{
return ;
}

It didn't fix the issue and i was still being redirected to the login page whenever I click on the reset password link, however when I made the following edit:

if(!isset($_GET['key'])  && !isset($_GET['login']))
    {
    return ;
    }

It works as intended but I'm confused why.

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.