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("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.