• Likes
  • Followers
  • Followers
  • Followers
  • Linkedin
  • Subscribe
Sign in

Welcome, Login to your account.

Forget password?
Sign in

Recover your password.

A password will be e-mailed to you.

  • Sunday, May 31, 2026
  • CONTENT POLICY
  • TERMS AND SERVICES
  • PRIVACY POLICY
  • SUBSCRIBE NOW

Latest Blog Latest Blog -

  • Home
  • WordPress
    • Beginners Guide
    • WordPress Plugin
    • WordPress Themes
    • WordPress Tutorials
    • Glossary
  • SEO
    • On-Page SEO
    • Off Page SEO
    • Local SEO
  • PHP
    • MySql
    • jquery
    • jquery & Javascrip
  • Information Technology
    • Codes
    • Google Updates
  • Tech & Gadget
    • Android
    • Apk
    • Best Picks
    • Console Commands
    • Money
    • Proxy
    • Proxy Sites
    • PSN
  • Game & Entertainment
    • Entertainment
    • Fallout 4
    • Gaming
    • GTA 5
    • Manga
    • Mods
    • movies
    • Music
    • Netflix
    • pokemon
    • PSN
    • Roblox
    • Roms
    • Top X
    • Torrents
  • Social Update
  • Contact Us
  • Add Blog In website
Latest Blog
WordPressWP Tutorials

Force Users To Login Before Reading Posts in WordPress

Last updated Jul 10, 2019
157
Share

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

Force Users To Login Before Reading Posts
157
Share
You might also like More from author
WP Tutorials

Why Your WordPress Site Lost Traffic (And How to Get It Back)

WP Tutorials

How to Sell on ChatGPT with WooCommerce (Agentic Guide)

WP Tutorials

Why Your WordPress Site Lost Traffic (And How to Get It Back)

WP Tutorials

How to Sell on ChatGPT with WooCommerce (Agentic Guide)

Prev Next

Popular Categories
  • WP Tutorials3300
  • showcase1193
  • News1153
  • Beginners Guide1134
  • Plugins1038
  • opinion536
OTHER SERVICES
  • Digital Marketing Agency In Toronto
  • SEO Company Toronto
  • Google Adwords management Toronto
  • web development company Toronto
  • Toronto Web Design Company
  • Social Media Agency Toronto
  • WIT Solution Canada
  • FacebookJoin us on Facebook
  • Google+Join us on Google
  • TwitterJoin us on Twitter
  • PinterestJoin us on Pinterest
  • LinkedinFollow us
  • HOME
  • CONTACT US
  • CONTENT POLICY
  • TERMS AND SERVICES
  • PRIVACY POLICY
  • BECOME AN AUTHOR
  • SITEMAP
© 2022 - All Rights Reserved.

Website Design & SEO Company in Ahmedabad by WIT Solution

SEO Company Toronto By WIT Solution Canada
sponsored