Johnny Chadda .se Things you didn't know that you should know

10Jul/096

Require login to view a Wordpress site

On a Wordpress site I have setup, I needed the ability to require user authentication before allowing anyone to view the website. To do this, just add the following snippet to the theme files:

get_currentuserinfo();
global $user_ID;
if ($user_ID == '')
{
        header('Location: wp-login.php');
}

Now you will be greeted with the Wordpress login page when entering the site without being authenticated! Note that if you want to be completely sure that no one can retrieve any information without authenticating, you need to either modify the Wordpress core files, or use some other type of authentication outside of the Wordpress code, such as a basic auth in the webserver.

Edit: I created a Wordpress plugin which does this in a much better way - WP Require Auth plugin released.

Comments (6) Trackbacks (1)
  1. I had better luck putting your code into:

    wp-blog-header.php

    right uder where wordpress loads:

    wp();

    Putting it in the template kept giving me errors that the header was already sent.

    • It’s probably best not to modify the core files since they will be overwritten on update. Are you sure that you are putting the code at the top of the theme files? Directly after <?php

  2. Thanks!
    I used this on my blog too, like ws0×9 I had better luck with it in the header file!

  3. “Note that if you want to be completely sure that no one can retrieve any information without authenticating you need to either modify the Wordpress core files, or use some other type of authentication outside of the Wordpress code, such as a basic auth in the webserver.”

    How would someone go about retrieving my information if I don’t use any outside authentication? I want to understand the vulnerabilities before I implement this.

    • You can get the posts using the RSS feed for instance and from there, retrieve the links for different post listings. I guess there could be a plugin available that does this in a better way. I might write one otherwise, when I have time.


Leave a comment


Optionally login with your OpenID or fill in the name and email below.