I am trying to use auth_redirect to automatically redirected not logged-in visitors when then visit a specific page. Here is the code I use:

function wpse16975_check_if_logged_in(){
    $pageid = 29;
    if(is_page($pageid)) auth_redirect();

The redirection works (I see the login page), but when I log in, I see the login page again. There is no way to access to the protected page because the browser keeps redirecting it to the login page.

I saw this question and its answer from @chrisguitarguy and modify my code to this :

function wpse16975_check_if_logged_in(){
    $pageid = 29;
    if(!is_user_logged_in() && is_page($pageid)) {
    $url = add_query_arg(

This code solved my problem : a logged in user can now access to the page. The question is : why cannot simply use auth_redirect ?

  • did you manage to solve this? This is happening to me too.. I am getting stuck on the login page. Thanks!
    – enriqg9
    Commented Jul 30, 2013 at 22:28
  • @enriqg9 : See my answer below and tell me if it solves your problem. Commented Jul 30, 2013 at 23:31

4 Answers 4


The problem is that this function is normally used in the backend. To use it in the frontend, you need to add the following filter:

add_filter( 'auth_redirect_scheme', 'wpse16975_check_loggedin' );
function wpse16975_check_loggedin(){
    return 'logged_in';

Then auth_redirect() will work as expected : redirect users to the login form if they are not logged in.

  • Cool! This fixed it for me too - weird it worked fine on another site without this fixed... thanks a lot!
    – andrux
    Commented Aug 19, 2014 at 0:35

See in the end of this

if (! is_user_logged_in())

The problem was, you put yourself into a loop. Your original code block, said that if I visit this page, send me to the login page. After logging in, then it went back to the page, which since you are now visiting the page, go to the login page. Round and round you go!

The new block first checks to see if you are logged in or not. If not, then and only then does it look to see what page you are visiting. If you are not logged in, then you get sent there and after logging in sent back. Now, since you are logged in, you get to see the page.

Make sense?

  • 1
    Have you seen what auth_redirect() actually does?
    – fuxia
    Commented Feb 29, 2016 at 16:04
  • The auth_redirect code that checks login, is or at least was, not working consistently. The secondary code bypassed the problem with the built in code by running the test separately.
    – G H
    Commented Feb 29, 2016 at 16:20

Been having problems with auth_redirect() only on mobile.

After four days - I wrote a solution that works, and doesn't need auth_redirect.

global $wp;
$current_url = add_query_arg( $wp->query_string, '', home_url( $wp->request ) );
if( !is_user_logged_in() ) { wp_redirect( home_url() . '/wp-login.php?redirect_to='. $current_url );

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.