Call Us Today! (866) 377-4331 or Request a Quote

Require Login for WordPress Pages

21 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.