How To Force Users To Log In And Restrict Access From Occasional Visitors

October 19, 2012 - 1 minute read

Ever wondered how to block access to your WordPress site for logged-out visitors? In my case, I needed to create a P2 installation that will be visible only to the┬ásite’s registered users. What’s going on here is pretty straight forward. First we check if the user is logged in using is_logged_in(). Then, if the user isn’t logged in, we call auth_redirect(), which is a core function that handles the redirect to the login page.

Seems like I could have worked fine by calling auth_redirect() alone, since this is what it says in core:

Checks if a user is logged in, if not redirects them to the login page

… But, when I tried to do that, I got an infinite redirection loop. Not really what I expected. So if any of you know a better way to do it, please share in the comments!