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.

WordPress Template is missing

Recently while working on a custom WordPress theme I kept running into the issue where my theme would become unusable and in my Appearance section of the back end I would get the error “Template is missing” for my theme.

Often you get this error when you are trying to inherit from a theme but the theme cannot be found,  in my case my theme wasn’t inheriting from another so I couldn’t work out why I was getting this error.

Eventually after a lot of careful cross checking I figured it out

I had to remove any comments that contained the string “TEMPLATE:”, this may also be caused by the lower case “template:” but I didn’t test this.

I had comments like this in as part of my stylesheet:

/* TEMPLATE: Sidebar content */

This was causing WordPress to look for a theme that didn’t exist.

I assumed that WordPress would only look in the top comment block for the theme information but i was wrong…