Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
WordPress Plugins
View all Guides


  1. steve says:

    I’m using Genesis with a child theme. The child theme does not have a header.php but the parent theme (Genesis) does. I tried putting the header.php code (above) into the parent header.php but it did not work. Also did not work with the functions.php code in the child or parent theme. Any thoughts on getting it to work with a child theme?

  2. Steven Stieng says:

    Hard coding ID’s in code is really REALLY bad solution.

    It means that every time you write a new post for members only, you need a programmer to update your code. You do not want to do that.

    Ray Gulick comment is a better option.
    Why? Because members are already registered users with a password, and only they can read the post once they have logged in.

  3. Ryan Chart says:

    Hello, this is great!

    However, when the user clicks the item, it doesn’t redirect to the user login page, but simply a blank white page? How would I fix this?

    • Editorial Staff says:

      If it’s not doing that, then please turn on wp_debug to see the error output. Maybe it’s conflicting with another plugin or code on your site.

      • Ryan Chart says:

        How can I do this? I tried changing the false to true in the wp_config file but nothing happened?

  4. Dan says:

    Does this work with pages as well as posts?

  5. Sam Hughey says:

    I’ve been looking for this and glad I found it. It works, but, I’m using the zM Ajax Login & Register plugin as a custom login page. How can I get this script to redirect users to the zM Ajax Login & Register login page instead of redirecting the user to the main WP login page?

    Sam Hughey

  6. Todd Gechtman says:

    This is a fantastic solution.
    What would be the full code to require login to specific categories.

  7. rotem says:

    how i can i block all the posts in my site with this code? (not plugin)

  8. David says:

    According to the codex for auth_redirect(), the function already checks if is_user_logged_in() before doing anything.

  9. Donal says:

    I tried it and it does not work. I am using wordpress 3.2. Any help would be great thanks.

  10. SuvechchhaSaha says:

    Can you tell me how can i force every user to login to read my post? Then what ID’s should i place on this $ids array?

    • wpbeginner says:

      @SuvechchhaSaha You don’t need to put any IDs. Your code would look like this:if (is_single() && !is_user_logged_in()) { auth_redirect(); }

  11. Jimmy says:

    Hello, Could you please clarify as to whether this code is valid for wordpress 3.1. Thank you

  12. Chad Warford says:

    Great tip, curious, is there a way to manipulate this code in order to have the entire site force visitors to login, as in they are not able to even arrive on the landing page until logging in?

    Thanks in advance. You are great.

  13. Brian p says:

    How would I make it category based instead of using the array of posts, it would make it so that I wouldnt have to update the array every time I wanted to add a new post that I want to be behind the login.

    I could just apply a category to the post that makes it force a login.


  14. Amanda says:

    This is an excellent script, thank you very much for sharing! Is there a way, instead of specific posts, to have the user log in to a specific category? Could you explain, if possible, how this code can be expanded to incorporate categories?
    Thank you so much..


  15. Ray Gulick says:

    Not sure I understand how this has a different effect than setting the visibility of a page to “password protected” on the “Add new page/Edit page” screen in WordPress admin. Can you explain why this function rather than simply selecting a checkbox?

    • Editorial Staff says:

      Many reasons. One password can get out of control. People might share it which will ruin the purpose of that page. This is so only registered members see the post. You can charge membership fee for your exclusive content using this trick and much more.

  16. Mad Mike says:

    Oh yeah. Everyone will get that :-)

  17. Vivek says:

    Thanks for the share. can it would be possible to put this code on thesis theme.

    • Editorial Staff says:

      You should be able to use it thesis as well because you are not really editing the theme here. You are adding a function in your custom functions.php file and then calling the function in your header hook for thesis.

  18. Jal says:

    Nice. What I want to ask is it possible to show to a guest user a blank post with a link like Please login to view. Then on the same page a pop up comes where the user logs in to WordPress and immediately the post shows up (using a bit of Ajax). This would be very handy.

    • Editorial Staff says:

      Very certain that it is possible, but not through this route because the auth_redirect feature actually redirects you to the login page period. No page in between.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.