Remove the “Private” and “Protected” prefix on blog posts

Some weeks ago on a client website, we were asked to remove the “Private: ” and “Protected: ” prefixes on a custom post type. If you don’t know what I am talking about, let me quickly explain the feature.

The visibility of a blog post

When you write a blog post, you’ll find the option “Visibility” in the “Publish” meta box. This is usually set to “Public”. The other two options are “Passwort proteced” and “Private”. When you select the first one, a blog post is only visible for logged in users with any role. The second option lets you set a single password for this blog post (this password is store plain text in the post edit screen).

Changes on the blog post titles

When you pick one of the other visibility setting, WordPress will automatically prefix the blog post title with “Proteced: ” or “Private: “, when you use the default functions such as the_title(). So when you don’t want to have the prefix, how could we remove it?

Removing the prefixes with filters

Remove the prefixes is rather easy. There the two filters private_title_format and protected_title_format to change the title of such blog posts. We use it as the following:

function remove_pp_prefix_title_format( $content ) {
    return '%s';
}
add_filter( 'private_title_format', 'remove_pp_prefix_title_format' );
add_filter( 'protected_title_format', 'remove_pp_prefix_title_format' );

Conclusion

So with these few lines, we can easily change the title for proteced and private posts. When I was asked the same questions in a Facebook group, I put this into a small plugin an published it as a Gist just to find out, that someone even had published a plugin on the official plugin directory. I hope you’ll find this useful for your own blog.

Posted by

Bernhard is a full time web developer who likes to write WordPress plugins in his free time and is an active member of the WP Meetups in Berlin and Potsdam.

Leave a Reply

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