Require Login for WordPress Pages

35 Comments
  1. You rock. It worked like a charm in a matter of minutes. I love you (metaphorically speaking).

  2. Thank you! I feel like I’ve been looking forever for the simple answer to having a private section of your website… everyone makes it so complicated. Thankyou. =)

  3. This is way better than all of the other solutions to show private pages in the sidebar. Thanks for your help man!

  4. This works most of the time but I find that I receive http 500 errors the first time a non-logged in user tries to open the page. On refreshing once or twice, the message in “wp_die” is then correctly displayed. It’s as if the “php else” code is skipped sometimes. Any ideas people? Thanks.

    • Hmm…500 errors can be any number of things. Are you using a caching plugin by any chance?

      • Hello. No caching plugin in use. I have worked around this by changing the php code to redirect to the login page if not logged in and then do an exit. This is not the same behaviour as in this article but works with complete reliability for me.

        Thanks for your reply.

  5. the solution worked fine, but wp_die caused my theme to display an error page layout, so i used echo instead…

  6. Thanks for that, worked a treat.

  7. Works great! However, I need to resolve the relative link to the login page for Windows 7. I will try it on a Linux box soon.

  8. Brilliant – thank you, can’t believe I spent 2 hours messing with crappy plugins that don’t work, so simple

  9. I had to use the ECHO as well, using wp_die caused a lay out error in my theme as well (only when user was not logged in of course).

    My question is, is there any way that once a user logs in, that they canend up on the page itself VS ending up on the dashboard?

    • I’m replying to the question I just asked about the log in redirect. I did some looking around and got this to work (note REST OF URL HERE means the rest of the URL to your specific page:

      [php]
      <?php else: echo(‘Sorry, you must first <a href="/wp-login.php?redirect_to=index.php/YOUR URL HERE/" rel="nofollow">log in</a> to view this page.’); endif; ?>
      [/php]

      • Looks great K! Thanks for that tip!

        • Is it possible to get the full line of code from the post for using the echo command instead wp-die?
          Your page cut-off the right side of it!

          I keep getting a parse error with the wp_die line.

          Thanks

          • The full code should be available. Perhaps you have javascript disabled…although that shouldn’t matter, that’s what formats the code blocks. Try view source if not. Any code I’d give you with echo would do the same thing if you can’t view a long line.

  10. Hiiii
    I’ve WordPress 3.1, with the theme lightword.
    I’ve used 1:1 the same code you advice. If the user isn’t logged in, the message comes, all fine. But if the User is logged in, only a white Page comes and in the developertools of Chrome i’ve also got http error 500. Not only Chrome, also Firefox and IE.

    Thx for support! =)

  11. Hi,
    What I’m looking for is that users get a login page. When they are logged in they should get redirected to this template page. Do you know how I can get this login page without letting the users getting acces to the dashboard?

  12. Thanks heaps…was a little unhappy when I realised making a page private hides from menus etc, but this fixed it up in 30 seconds.

  13. Thanks for clarifying “private” visibility on a wordpress page. I was hoping it allowed logged-in users to view those pages.

  14. Hi John,

    I know you posted this quite a while ago but it has helped me in arriving at a relatively simple solution for securing individual pages behind a required login.

    I did create a new template and the page requires a login now but as soon as a user logs in they are taken to the WP dashboard. Obviously that’s not a good thing.

    Do I need a new plugin just for registering users to this page? If no how do I keep the user from getting to the dashboard?

    thanks.

    • Hey Randy, I’m pretty sure a user will always be able to get to the dashboard if they’re logged into WordPress…that’s just the way WP works. They won’t be able to do much if they’re just a subscriber. I’m sure there’s a way to redirect after login. Google something like “wordpress redirect after login” and you should find some info on that.

  15. Thank you for the very easy and straightforward solution to getting a login dialog onto a page template. Very straightforward and user friendly for site admins.

  16. Hi John,
    Read your tutorial above as i was interested in achieving a similar result.
    I did notice during testing that you can be logged into any wordpress site at all and still gain access.
    Just thought i would let people know. Maybe further tweaking is required. Thanks anyway this has got me closer to my goal.

    • Hmmm…that shouldn’t happen at all. is_user_logged_in() should only return true if the user is logged into the current site…it would be based on the login credentials, which are specific to each site…unless two sites are sharing login auth or something.

    • Hi Weeksy, I tested the option above as I am using this on a clients site and it works well. If you don’t register or login no one can view that page. I am sure there must be another plugin in your site that produces what you are saying.

      Kind regards

  17. This is a great tip!

    I wanted to keep the theme, so I added the if statement in the post-content div, and just echoes a text directing to the login/register page instead of usng wp_die.

  18. Hi John,

    I though this was not easily possible without a plugin but you have shown how to do this in less than 10 steps. !!!

  19. It is so awesome ! THANKS !

    I was about to do it with an heavy plugin.. but your method took about 3 lines of code and works perfectly :)

  20. It’s much easier to just use the auth_redirect() function which checks if they are logged in, if not takes them to the log-in page, and after returns them to the page they came from.

    • True, but here we’re displaying a message to the visitor rather than sending them straight to a login page with no message about why.

  21. Hi John,

    The code is working perfectly no doubt. But what i need is once you are logged in entering the credentials… you should be redirected to the same post/page from where we took him to the login page… currently it is taking to the user profile… hope that made some sense…

    Many thanks,
    Ron

  22. Thanks this worked great for a site I am working on.

Leave a Reply

Address

Razorlight Media
Phone: 513-549-7355
Fax: 866-377-4331
Url:
cash, check, credit card, invoice, paypal
2207 Kemper Lane
Cincinnati, OH 45206
700 West Pete Rose Way, Suite 554
Cincinnati, OH 45203