Controller functionality – if user is not logged in send them to specific page (not wp_login)

This sounds dead bang simple, but I just can’t figure it out.

I want the following functionality:

Read More

If user is not logged in and they try to access anywhere on the site (including the homepage) – send them to a specific page.

I tried, adding the following but received a “not redirecting properly” message:

if (!is_user_logged_in() ) {
 wp_redirect ('/public' );
 exit;
 }

I guess if I can add a clause to the if statement saying and the page I am accessing is not the public page, but I’m not quite sure how to that properly.

Any plugins that I see out there redirect to the wordpress login screen.

Any help is greatly appreciated!

Thanks,

MG

Related posts

Leave a Reply

1 comment

  1. The reason why you are getting a not redirecting properly message is because you are creating an endless loop of redirects. They get redirected to /public but because they are not logged in they get redirected again and again and again…

    Try this code instead:

    if( ! is_user_logged_in() && ! is_page("public") )
    {
        wp_redirect( site_url("/public") );
        exit;
    }
    

    I’m assuming that /public is a page you have setup.