Making WordPress Private

Every once in a while, it may be necessary to build a WordPress-powered site only accessible to registered users. Interestingly, a very simple theme hack can take care of this. All you need to do is to include the following PHP code into the current theme so that it is executed before the output begins:

global $user_ID; 
get_currentuserinfo(); 
if ($user_ID == '') { 
  header('Location: ' . 
    get_bloginfo('wpurl') . 
    '/wp-login.php?redirect_to=' . 
    rawurlencode($_SERVER["REDIRECT_URL"])); 
  die(); 
}

Generally, a good place to put this code is the header file, although this will ultimately depend on the theme.

Essentially, this snippet checks if the user is logged in and if not, redirects the browser to the login page, while also telling the login page what page the user wanted to retrieve, so that the user could be redirected there after successful login.

A later addition: Sure enough, someone actually created a plugin that addresses this very problem…

This entry was posted in Random, Technology. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *