Free Wordpress Blog Setup

Force Users To Login Before Reading Posts in WordPress

By Rilwis in Tutorials
Force Users To Login Before Reading Posts in WordPress

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.

WordPress: Force Users To Login Before Reading Posts

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.

Tuan AnhTuan Anh (aka. Rilwis) is a 25 years old blogger and web developer from Vietnam. He is interested in PHP, MySQL as well as Web 2.0 technologies (CSS, Javascript, Ajax). He loves WordPress and all related to it. Follow Rilwis on Twitter to stay update with him. Rilwis’s website: Deluxe Blog Tips

What Next?

Digg it
Save This Page
Subscribe to WPBeginner
Stumble it
Free Wordpress Blog Setup

Comments

12 Responses to “Force Users To Login Before Reading Posts in WordPress”
  1. 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.

  2. Vivek says:

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

  3. Mad Mike says:

    Oh yeah. Everyone will get that :-)

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

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

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

  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?

Share Your Opinions

Tell us what you're thinking...
and if you want a pic to show with your comment, then get gravatar!

Please make sure that you have read our Comment Policy.

Due to high volume of request from our readers, we are adding this feature that allows you to stay updated with this post's comments without having to participate in the discussion even though we would love your input as always. Don't worry we hate SPAM just as much as you do, so you will never receive any SPAM messages from our site and that's our promise to you.

Subscribe without commenting

Close Bar