Skip to content

Instantly share code, notes, and snippets.

@lstellway
Created April 5, 2019 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lstellway/46444127ebcfe7d1f38c1f3a741e66c6 to your computer and use it in GitHub Desktop.
Save lstellway/46444127ebcfe7d1f38c1f3a741e66c6 to your computer and use it in GitHub Desktop.
<?php
/**
* Force Login
*/
function force_login() {
if (!is_user_logged_in()) {
$url = home_url($_SERVER['REQUEST_URI']);
$whitelist = [];
// $whitelist = apply_filters('forcelogin_whitelist', []);
$redirect = apply_filters('forcelogin_redirect', $url);
$login = wp_login_url();
$pattern = '/\?.*/';
if (preg_replace($pattern, '', $url) != preg_replace($pattern, '', $login) && !in_array($url, $whitelist) ) {
wp_safe_redirect(
wp_login_url($redirect),
302
);
exit;
}
}
}
add_action('init', 'force_login');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment