If your blog has some restricted area that you don’t want to publicize to all visitors, just for members only, then you might want to force users to login before reading these posts. Fortunately, WordPress has a built-in function which can help us to do that.
The function is auth_redirect(), this is how it works: When it is called from a page, it checks to see if the user viewing the page is logged in. If the user is not logged in, they are redirected to the login page. The user is redirected in such a way that, upon logging in, they will be sent directly to the page they were originally trying to access.
By using this function, we can implement our code that check if post is restricted or not, and redirect users to login page if needed.
Just paste the following code into your theme’s functions.php file:
function my_force_login() { global $post; if (!is_single()) return; $ids = array(188, 185, 171); // array of post IDs that force login to read if (in_array((int)$post->ID, $ids) && !is_user_logged_in()) { auth_redirect(); } }
Change the array of post IDs to fit your requirement. After that, open the header.php
file and put the following code in the very top:
<?php my_force_login(); ?>
The code is simple, but you can expand it with more options like: require login in some specific categories, make an option page for easy input post IDs, etc.
The function auth_redirect() is available since WordPress 1.5.

how can i use custom redirect page function for login or registration page ?
I want to restrict all posts in my website. If I am using categories should I have to include the category slug or Category ID.
How to add new posts automatically to the array
For some reason, I could only get this to work by changing
if (!is_single()) return;
to
if (!is_singular(‘page’)) return;
You can also just remove that line, although I guess it might slow the site slightly.
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?
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.
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?
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.
How can I do this? I tried changing the false to true in the wp_config file but nothing happened?
Does this work with pages as well as posts?
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?
Thanks,
Sam Hughey
Hi,
This is a fantastic solution.
What would be the full code to require login to specific categories.
how i can i block all the posts in my site with this code? (not plugin)
According to the codex for auth_redirect(), the function already checks if is_user_logged_in() before doing anything.
I tried it and it does not work. I am using wordpress 3.2. Any help would be great thanks.
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?
@SuvechchhaSaha You don’t need to put any IDs. Your code would look like this:if (is_single() && !is_user_logged_in()) { auth_redirect(); }
Hello, Could you please clarify as to whether this code is valid for wordpress 3.1. Thank you
Yes, it should work.
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.
Using a membership plugin would be a good idea.
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?
You would have to modify the codes slightly from if is_single to if_is_category… You can also use a membership plugin for this (a recommended option).
What membership plugins would you recommend for this?
Wishlist or Magic Members
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
Get a membership plugin, such as Wishlist or Amember.
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?
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.
Oh yeah. Everyone will get that
Thanks for the share. can it would be possible to put this code on thesis theme.
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.
thanks for it i will do it on first testing site and then implement it on real site
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.
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.