1

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.

2

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();
    }
}
  • What file should I add this in? Also, I also want to redirect users to my register page.. How can I do that? – Omar Qazi Oct 29 '16 at 16:26
  • I added it in the functions.php. I just wanna know how to redirect to the register page too. – Omar Qazi Oct 29 '16 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. – Prasad Nevase Oct 29 '16 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. – Omar Qazi Oct 29 '16 at 17:07
  • Can you share website link by any chance? – Prasad Nevase Oct 29 '16 at 17:13
0

try this code

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

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() ) {
    auth_redirect();
}

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. – Omar Qazi Oct 30 '16 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 '16 at 11:33

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.