Remove Private/Protected from Post Titles

Posted on: February 3, 2010 by Chris Coyier

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.

Thumb for Remove Private/Protected from Post Titles

Let's talk it out, folks.

  1. An improvement would be to search for __("Protected") so it’s even language independent :)

    #1
  2. This is filterable since WP v2.8, so it’s even easier:

    add_filter('protected_title_format', 'blah');
    function blah($title) {
           return '%s';
    }

    Is that what you were after or did I miss something?

    #2
  3. I’m really absent-minded sometimes. :-D Forgot to add that the same filtering works with private_title_format too.

    #3
  4. Is this code work for on pages?

    #4
  5. Matthew said on July 13, 2012:

    Why, instead of <?php the_title(); ?>, don’t you use <?php echo $post->post_title; ?>? It worked for me :).

    #5

Comments are closed. If you have something really important to add, contact us. Thank you!