Leave a Reply

4 comments

  1. 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.

  2. 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?

  3. 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 );