Redirect to login if user is not logged in

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.

https://gist.github.com/daankortenbach/3878390

The Gist above could be out of date, development of this plugin is done on Github. Latest stable version on the wp.org repo.

Update 2016-09-09:
I will no longer maintain this plugin, contact me if you want to adopt it.

Update 2014-01-31:
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.

Update 2014-01-31:
Version 1.4 now available in the WordPress repository.
Changes: Moved the conditionals to the init hook due to some edge cases not redirecting.

Update 2013-06-09:
The “Redirect to login if user is not logged in” plugin is now hosted on the official WordPress Repository.

Reader Interactions

Comments

  1. Please I do really any help from you :
    this website http://cabcongo.org is by anyway accessible in the public view When I try to go at index page public iew it’s redirect me at wp_login.php page (authentification and user’s pass) How can I solve this pb ???? #SOS
    Thanks for all !

      • I made so but it kept redirecting me at wp_login.php by anyway ; has he wrote any informations that lead to this in my database ?????

        • That shouldn’t happen. It doesn’t write anything to the database. Do you have any caching plugins active? Or other plugins that redirect, maybe Members?

          Try deleting my plugin to be sure.
          Try purging (opcode) cache if you use it.
          Try disabling other plugins to see if any of them are causing this.

          • I only got social media plugin and pretty list PDF I disactivate those but keep stucking on this jump from index to wp_login.php ; I looked inside my .htaccess there is any problem with that cause I only put SetEnVPHP 5.2.3. inside … I a little bit lost

          • While you should run recent WordPress versions on PHP 5.3 or higher I can’t see how that could be causing the erronous redirect.

            Did you put any code in functions.php?
            Search for “redirect” in your theme and plugin files.

            Other then that, I can’t help you. Good luck and maybe try the WordPress support forums: http://wordpress.org/support/

  2. Hi Daan,

    I’ve been searching for a plugin like this.

    I see there are a few versions 0.1 and 1.0. I’ve tried both with the same result. After redirecting me to login, and after I login, I select ‘Visit site’ in my wordpress admin window, and it redirects me to login again. Not sure if I’m using the wrong version or if I’m not properly upgrading the php code in the file.

    Would appreciate any assistance. Would really like this to work.

    • Hi Jun, I’m not sure why that is happening on your installation. Possibly there is something at work that keeps logigng you out.

  3. Looks really lightweight and essentially does what I want; however, there is one issue that is stopping this from being the plugin for my current need – this applies to every page on the entire site. Is there anyway to have this set to where it can only be one or 2 pages? I basically have 1 page that i need this functionality on. I tried to implement the code directly in my template page (and child functions.php) but it did not seem to work.

    Mike

    • Ah yes, this plugin does exactly what it says it does: Redirect to login if the user is not logged in.

      However, I’m getting this request quite often so I’ve decided to add this functionality in the next version.

      • I also need to restrict just a few pages (and bbpress) to registered users. Any update on an update?

        • Sorry no updates on an update other than I’ve done some work on it. This feature will add a huge amount of code to the currently tiny code and as I’m doing this in the little free time I have it’s taking a while.

          You might take a look at the membership plugin reviews by Chris Lema:
          http://chrislema.com/reviewing-woocommerce-memberships/

          There’s a list of 27 membership plugin reviews in that post.

  4. It could be realy usefull if it could wait redirecting people until they click anything on the frontpage also the load time would be much faster since admin-ajax.php taking up much of the load time.

    • I’m not sure what you are getting at. Nothing is loaded because the redirect is a header, if anything would load the redirect would not work.

Leave a Reply

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