Only Showing Content & Comments To Registered Users In WordPress

Recently one of my clients asked if it was possible to only show content to registered users on a WordPress site.

The solution is very simple using WordPress’ filter system.

add_filter("the_content", "block_content" );
add_filter("comment_text", "block_comment");

function block_content($content){
if(!is_user_logged_in()){
return __( ‘You must be logged in to view content’ );
}else{
return $content;
}
}
function block_comment($content){
if(!is_user_logged_in()){
return __( ‘You must be logged in to view comments’ );
}else{
return $content;
}
}

Putting that snippet in your theme’s functions.php file will swap comment, page and post content with the messages above.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>