If you’re running a private blog and you want to make sure all logged-out visitors are redirected to the sign-in page, this will do the trick.
add_action( 'template_redirect', 'logged_in_users_only' );
function logged_in_users_only() {
if ( !is_user_logged_in() )
wp_redirect( wp_login_url() );
}
If you want users to come back to the front page after logging in, you can pass an argument to the wp_login_url() function.
add_action( 'template_redirect', 'logged_in_users_only' );
function logged_in_users_only() {
if ( !is_user_logged_in() )
wp_redirect( wp_login_url( home_url() ) );
}
This will only redirect users who were initially redirected to the login screen from the front-end. If the user went directly to the login screen – the filter above won’t have been triggered, so they’ll be taken to the administration side.