WordPress | Hide SIte to non logged user

I need an easy way for switch from a “Logged user only” blog to a “Open to all Blog”.

The easiest way is to add a control on function.php so, open function.php and add:

/* Site visible only for logged user */

function myCourtesyPage() {
  $idCourtesyPage = 2814; 
  if (! is_user_logged_in() ) {
    if ( get_the_ID() <> $idCourtesyPage ) {
      wp_safe_redirect(get_permalink($idCourtesyPage)); 
      die();
    }
  }
}        
add_action( 'template_redirect', 'myCourtesyPage' );

The variable ” $idCourtesyPage ” have as value the id of the page that contain message for “non logged user”.
Basically, we check if user is logged, if so, view the request page, on other side, if isn’t logged redirect to the ” $idCourtesyPage ” page.

If we need to deactivate the control, we can comment the line

add_action( 'template_redirect', 'myCourtesyPage' );

This stop the control.

That’s all