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

Comments

  1. David says:

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

  2. Donal says:

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

  3. 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(); }

  4. Jimmy says:

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

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

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

    thoughts?

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

    Amanda

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

  9. Mad Mike says:

    Oh yeah. Everyone will get that :-)

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

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