DiggingIntoWordPress

by Chris Coyier & Jeff Starr

Remove Private/Protected from Post Titles

Posted by on

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.

Update 2014/01/27: For a plugin-based solution, check out Remove Protected by Aubrey Portwood.

7 Responses

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

  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?

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

  4. Is this code work for on pages?

  5. Matthew July 13, 2012

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

Comments are closed. Contact us with any critical information. Thank you!

Code is poetry