Redirect Visitors To Login (Force Login Page)
March 14, 2017
If you want to create a login based website and you need to redirect all your visitors to login page, please add this code in your theme’s functions.php file.
Solution 1
In Hide My WP Ghost you can add Login/Logout redirects based on user role.
Follow this tutorial to learn how to setup redirects with Hide My WP Ghost plugin:
https://hidemywpghost.com/kb/activate-security-tweaks/#redirect_on_login
Solution 2
function protect_whole_site() { if (!is_user_logged_in()) { if (isset($_SERVER['REQUEST_URI'])) { $url = untrailingslashit($_SERVER['REQUEST_URI']); if (strpos($url, HMWP_Classes_Tools::getOption('hmwp_login_url')) === false) { wp_redirect(site_url('wp-login.php')); exit(); } } else { wp_redirect(site_url()); exit(); } } } add_action('template_redirect', 'protect_whole_site');
If you have Ghost mode activated in Hide My WP Ghost, the redirect will to the new login path.
Solution3
If you want to redirect all the logged subscribers to front-end you need to add this code in your theme’s functions.php file.
function no_dashboard() { if ( current_user_can( "subscriber" ) ) { wp_redirect(home_url()); exit(); } } add_action('admin_init', 'no_dashboard');
In this case, the subscribers will not see your WordPress dashboard which is a good security workaround.