You can also use the following plugin: Private WP.

If you want to have a private WordPress site, for example while still developing it, you might want to redirect all not logged in users to the WordPress login page. Using the following code every visitor who is not logged in will be redirected to wp-login.php. All authenticated users can work and read the site normally.

Use-cases

  • Website is in development
  • You want only authenticated members to be able to see your blog.

Code

Place the following code in your functions.php file.

<?php

//This function redirects all not autheticated users
function loggedin_users_only() {
	global $pagenow;
	//check if the current page is not the wp-logon.php page
	//check if the current user is logged in
	if(!is_user_logged_in() && $pagenow != 'wp-login.php') {
		auth_redirect();
	}
}

//use the wp hook to load this function on every page load
add_action( 'wp', 'loggedin_users_only' );

?>

Picture: https://wordpress.org/about/logos/