0

I set up a simple site about a year ago for a client that uses a modified Twenty Fourteen theme to run a members only site. You can see the site here.

She wanted the site to be completely restricted to members only and I had success using the Restricted Site Access plugin. It worked great, but recently began to redirect back to the login page with each click on the site. Users remain logged in, but regardless, they are returned to the log in page.

I found a code solution here and implemented it, but it's doing the same thing:

add_action( 'template_redirect', 'wpse16975_check_if_logged_in' );
function wpse16975_check_if_logged_in() {
    if( !is_user_logged_in() ) {
        $url = add_query_arg(
            'redirect_to',
            get_permalink( $pageid ),
            site_url( 'wp-login.php' )
        );
        wp_redirect( $url );
        exit;
    }
}

Is there a simple, non-plugin solution to keep the site restricted and redirect non-logged in users to the login page? I've tried removing plugins, using different themes, etc. Seems fairly simple, but can't find a solution.

0

You are using an undefined variable in your code ($padeid), it can be causing you problems. Try this code instead:

add_action('template_redirect','wpse16975_check_if_logged_in');
function wpse16975_check_if_logged_in(){

    global $wp;

    if( ! is_user_logged_in() ) {

        $url = wp_login_url( site_url( add_query_arg( array(), $wp->request ) ) );
        wp_redirect( $url );

        exit;

    }

}
  • Thanks, but it's still the same thing- back to the login page when I click on an item in the content section. Here's a screenshot screencast.com/t/CyIZk6LKXzSt – fmbns May 24 '15 at 10:10
  • The code posted is working here: if the user is not logged in, he/she is redirect; otherwise he/she can see the content. Impossible to say what happen in your site without more information or a closer debug in your code. – cybmeta May 24 '15 at 10:13
  • Thanks anyway- I'm getting no errors and the problem started sporadically. It's a real head scratcher. – fmbns May 24 '15 at 11:18

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.