Remove Private/Protected from Post Titles
I had the situation come up where I need a password-protected post in WordPress. Of course that is super easy in WordPress, you can set up a password for it right in the “Publish” box before publishing. But by default, WordPress appends “Protected: ” to the front of the post title, before and after the password has been entered. I didn’t like that, and thought that the password box was clue enough that the material was password protected.
I ran across some simple code in the official WordPress forums with a solution (thanks, t31os_!).
function the_title_trim($title) {
$title = esc_attr($title);
$findthese = array(
'#Protected:#',
'#Private:#'
);
$replacewith = array(
'', // What to replace "Protected:" with
'' // What to replace "Private:" with
);
$title = preg_replace($findthese, $replacewith, $title);
return $title;
}
add_filter('the_title', 'the_title_trim');
I saw a plugin to do this too, but I think since this is rather theme-specific, functions.php code makes the most sense.
7 responses
-
An improvement would be to search for
__("Protected")
so it’s even language independent :) -
This is filterable since WP v2.8, so it’s even easier:
function blah($title) { return '%s'; }
add_filter('protected_title_format', 'blah');
Is that what you were after or did I miss something?
-
I’m really absent-minded sometimes. :-D Forgot to add that the same filtering works with
private
_title_format
too. -
Is this code work for on pages?
-
Why, instead of
<?php the_title(); ?>
, don’t you use<?php echo $post->post_title; ?>
? It worked for me :)