Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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);
share|improve this question

1 Answer 1

up vote 0 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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