Skip to content
Wordpress, WooCommerce, Php, Html, CSS
  • Home
  • Tutorials
  • WordPress Courses
  • Plugins
  • Showcase
  • News
  • Contact

Force Users To Login Before Reading Posts in WordPress

January 30, 2021 by admin

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
Categories Tutorials Tags login before reading posts in WordPress, login forced wordpress posts, membership posts in WordPress, password protected wordpress posts
Post navigation
Giveaway – Get Your Free Copy of Templatic WordPress Themes
Giveaway – Get your Free Copy of ThemeJam WordPress Themes

Product Highlight

This first widget will style itself automatically to highlight your favorite product.

Learn more

Recent Posts

  • How to Prevent Invalid AdSense Clicks in WordPress (Step by Step)
  • How to display full-size images in WordPressHow to Display Full Size Images in WordPress (4 Methods)
  • How to add your social media feeds to WordPressHow to Add Your Social Media Feeds to WordPress (Step by Step)
  • What's coming in WordPress 5.7What’s Coming in WordPress 5.7 (Features and Screenshots)
  • Requiring email address for file downloads on your WordPress websiteHow to Require an Email Address to Download a File in WordPress
  • Flatsome vs WoodMart vs Xstore vs Elementor Comparison
  • Elementor Addons and Themes
  • Elementor Pro Nulled
  • 10 Best WordPress Themes of 2021
  • Yoast Seo Nulled
  • Best Elementor WordPress/WooCommerce Themes
  • Trending Plugins/Addons and Themes for WordPress
  • Top 10 WordPress E-Commerce Themes
  • Woocommerce Plugins and Themes
  • Privacy Policy
  • Terms
  • Contact
© 2021 Wordpress, WooCommerce, Php, Html, CSS • Built with GeneratePress