Latest Tech and Tips

Force Users To Login Before Reading Posts in WordPress

0

If your blog site has some limited location that you do not wish to advertise to all site visitors, simply for participants just, after that you could wish to force users to login before reading these posts Fortunately, WordPress has actually a developed-in feature which can aid us to do that.

WordPress: Force Users To Login Before Reading Posts

The feature is auth_redirect(), this is exactly how it functions: When it is called from a web page, it inspects to see if the customer seeing the web page is loggedin If the customer is not logged in, they are rerouted to the login web page The customer is rerouted in such a manner in which, upon logging in, they will certainly be sent out straight to the web page they were initially attempting to gain access to

By utilizing this feature, we can apply our code that check if article is limited or otherwise, as well as reroute users to login web page if required.

Just paste the complying with code right into your style’s functions.php data:

feature my_force_login() {
worldwide $post;.

if (! is_single()) return;.

$ ids = variety( 188, 185, 171);// variety of article IDs that force login to review.

if (in_array(( int)$ post->> ID, $ids) && & &! is_user_logged_in()) {
auth_redirect();.
}
}

Change the variety of article IDs to fit your need. After that, open up the header.php data as well as placed the complying with code in the really leading:

<