Hide the Entire Site Behind a Login Wall

Description/Instructions

Natively, Voxel cannot do a site wide visibility to make it so the entire site is behind a login wall, this snippet does that. Change the URL as needed.

If this snippet helped, feel free to buy me a taco :)

 

function redirect_non_logged_in_users() {
if (!is_user_logged_in() && !is_page('login-registration')) {
$current_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$encoded_url = urlencode($current_url);
error_log('Redirecting non-logged in user from: ' . $current_url); // Optional: for debugging
wp_safe_redirect('https://staging.voxel.guide/login-registration?redirect_to=' . $encoded_url); //BE SURE TO CHANGE THIS URL
exit;
}
}
add_action('template_redirect', 'redirect_non_logged_in_users');

function custom_login_redirect($redirect_to, $request, $user) {
if (isset($user->roles) && is_array($user->roles) && !empty($_REQUEST['redirect_to'])) {
return esc_url($_REQUEST['redirect_to']);
} else {
return admin_url();
}
}
add_filter('login_redirect', 'custom_login_redirect', 10, 3);

 

  • PHP
Copy Code

Let's Chat About this Snippet