I create a template page as seen below to check if user is logged in or not. If user is not logged in, I want to redirect user to first login page and after login I want him to redirect previous page.

With the code below, Login redirection function redirects to login page again after user login. How can I fix that code?

In my template page :

      wp_redirect( 'http://....../wp-login.php', 302 );
      wp_redirect('----------otherpage---',302 );

in functions.php :

    function directtorequest( $redirect_to, $request, $user )
        // instead of using $redirect_to we're redirecting back to $request
        return $request;
    add_filter('login_redirect', 'directtorequest', 10, 3);

1 Answer 1


Add redirect_to as a query variable in your link whenever you redirect user wp-login.php. Like

wp_redirect('http://...../wp-login.php?redirect_to=your_previous_page_link', 302);

In redirect_to write the link of any page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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