How to restrict an entire WordPress site to visitors, but the register and login page?

Restrict Website, But Login and Register Page


I want my my entire WordPress site to be restricted to visitors, but I want the register and login page to be accessible (not restricted) to the visitors

Since my website is a members-only website, I want to restrict it’s access so that visitors only see 2 pages, the login page and the register page.

I have tried multiple plugins to do exactly that but unfortunately for me, I haven’t been able to find the right one. Some plugins redirect the entire website to one page while other plugins require custom redirection settings for each page to be separately added.

What I want:-

  1. Restrict entire WordPress website to visitors by redirection.
  2. Do not restrict 2 pages, Login Page and Registration Page.
  3. Redirect Users to Login Page and/or Register Page.

There is an option in my Login Page to view the Register Page.
I want both of these pages to be accessible to the visitor and the rest of the website to be inaccessible.

3 Answers
3

Below code will work with WordPress default login/register screens:

add_action( 'wp', 'member_only_site' );
function member_only_site( ) {
    if ( ! is_user_logged_in( ) ) {
          auth_redirect();
    }
}

Leave a Comment