What is the easiest method to make all my articles accessible only to registered users?
My homepage contains titles and excerpts but I want to allow access to the full content only to registered users.
I remember there was an admin setting for that but I cannot find it.
Maybe Im wrong. Any ideas?
Thanks.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You could hook into template_redirect, check if it is a singular page (post, page, custom post type) and force a log in:
add_action( 'template_redirect', 'login_to_see_content' );
function login_to_see_content()
{
if ( is_singular() && ! is_user_logged_in() )
auth_redirect(); // does nothing for logged in users
}
As suggested by @s_ha_dum in the comments, an additional ! is_user_logged_in() might be necessary. In theory, it shouldn’t (and it isn’t in my setup).
Method 2
I recommend a plugin like User Access Manager for that purpose. It gives more function and control than using native function such as making posts Private.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0