Change WordPress user roles and capabilities › Forums › Restrict or Permit access inside WordPress – how to › Redirect visitors to login page › Reply To: Redirect visitors to login page
27/11/2019 at 15:11
#6157
Vladimir
Keymaster
Hi,
Content view restrictions add-on includes ‘Logged in only” and redirection action for selected posts/pages in case of access error. If redirection URL field is empty, visitor is redirected to login page.
URE does not have similar option for a whole website. You need install another plugin or custom code for this purpose, like this:
// Redirect users who arent logged in...
function members_only() {
global $pagenow;
// Check to see if user in not logged in and not on the login page
if( !is_user_logged_in() && $pagenow != 'wp-login.php' )
auth_redirect();
}
add_action( 'wp', 'members_only' );
Code sample was taken here.