Redirects anyone how isn’t a logged in user to the absolute URL in the code.
This code redirects WordPress front page to login page if user logged.
/**
* Redirect Logged Out Users To The Login Page
*
* @uses template_redirect() Template Redirect action hook
* @uses is_user_logged_in() is user logged in conditional tag
* @uses wp_redirect() WP Redirect function
*
*/function w3make_redirect_login_page() {
if ( ! is_user_logged_in() && is_front_page()) {
wp_redirect( 'http://www.wordpress.dev/wp-login.php' );
exit();
}
}
add_action( 'template_redirect', 'w3make_redirect_login_page' );
In this case, WordPress website homepage for all logged out users will be redirected to the login page however this URL can be changed to any URL.
Remove && is_front_page()
from ‘if’ condition…
Then above code can be used to make your site into a membership site and require logged out users to register before being able to view your sites content.
To redirect the homepage to the WordPress login page when users are logged out, follow these steps:
1 Install and activate the “Peter’s Login Redirect” plugin.
2. Go to Settings and click on “Login/logout redirects.
3. In the “Redirect URL” field, enter the URL of your WordPress login page usually “/wp-login.php”
4. Check the box next to Redirect users not logged in. option.
5. Save your changes.o
Now when users are logged out they will be automatically redirected to the WordPress login page when they visit your website’s homepage.