0

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 :

    <?php
    if(!is_user_logged_in()) 
    {   
      wp_redirect( 'http://....../wp-login.php', 302 );
    }
    else
    {   
      wp_redirect('----------otherpage---',302 );
      exit;
    }
    ?>

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);
0

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, privacy policy and cookie policy

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