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( ) ) {
  • What file should I add this in? Also, I also want to redirect users to my register page.. How can I do that? Oct 29, 2016 at 16:26
  • I added it in the functions.php. I just wanna know how to redirect to the register page too. Oct 29, 2016 at 16:28
  • By default it takes user to WordPress default login page. If you enable "Anyone can register" option from Settings > General then you will see a register link on login screen. From user experience perspective I think taking user to login & then showing there registration link is just perfect. Because those who have already registered can directly login & those who have not registered, can register on the site by clicking on Register link. Oct 29, 2016 at 16:56
  • I did enable Anyone can register but when I click on register from the login page it redirects to the login page again. Oct 29, 2016 at 17:07
  • Can you share website link by any chance? Oct 29, 2016 at 17:13

try this code

function redi() {
global $pagenow;
if (!is_user_logged_in() && $pagenow != 'wp-login.php' && $pagenow !='wp-login.php?action=register') {
}}  add_action('template_redirect', 'redi');

A possible alternative:

global $pagenow;

$accessible_pages = array('wp-login.php', 'wp-login.php?action=register');

if ( !is_user_logged_in() && !in_array($pagenow, $accessible_pages) && !is_admin() ) {

You can insert in the array "accessible_pages" the pages (separated by commas) that you want to be public-accessible to visitors, avoiding redirects to the login page every time that a page is loaded.

  • not working, redirecting to login page when I go to the register url. Oct 30, 2016 at 2:29
  • It wasn't working because you needed to change the "register" page to match yours. Changed the array for you, assuming that you're using the default wordpress registration page; try it.
    – BlackOut
    Oct 30, 2016 at 11:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.