Sometimes I see other developers use bloated solutions for something very simple. Recently I saw the use of the Members plugin just to redirect users to wp-login.php (or whatever the login page is set to) if users are not logged in. That counts as a bit of overkill in my world. So, here is a small plugin to redirect to wp-login.php if user is not logged in.
You can download the plugin from the WordPress Plugin Repository.
The Gist above could be out of date, development of this plugin is done on Github. Latest stable version on the wp.org repo.
I will no longer maintain this plugin, contact me if you want to adopt it.
Version 1.5 now available in the WordPress repository.
Changes: Complete rewrite to use the already existing auth_redirect hook. Strips ‘?loggedout=true’ from redirect url after login.
Version 1.4 now available in the WordPress repository.
Changes: Moved the conditionals to the init hook due to some edge cases not redirecting.
The “Redirect to login if user is not logged in” plugin is now hosted on the official WordPress Repository.