Change WordPress’ Default Password Protected Text

Change WordPress' Default Password Protected Post Text

WordPress has a great feature that allows you to password protect individual posts from site visitors. To view the posts all your users need is a password. No annoying registration and email confirmation to mess with. While this feature works great out of the box, perhaps you may want to spice things up a bit by adding a custom message to tell the user some information other than the default:

“This post is password protected. To view it please enter your password below:”

Changing this default text is easy using WordPress filters

If up would like to change this text to let people know how they can obtain a password. Where do I make these changes. I don’t see it in the theme files.

How to Modify the Default Text

We will be using WordPress Filters to change the default password protected post function the_password_form. By using WordPress filters we effectively sidestep modifying the core files and ensure that our theme is that much more cool.

First, backup your theme’s functions.php file

It’s always a good idea to have a backup ready in case something doesn’t go as expected and you have to revert back to an older version. If you don’t have a development environment and are coding commando then make sure you backup your theme’s function.php before doing any editting.

Add the Following Filter to your theme’s functions.php file

<?php
add_filter( 'the_password_form', 'custom_password_form' );
function custom_password_form() {
	global $post;
	$label = 'pwbox-'.( empty( $post->ID ) ? rand() : $post->ID );
	$o = '<form class="protected-post-form" action="' . get_option('siteurl') . '/wp-pass.php" method="post">
	' . __( "This post is password protected and this is what I want to say about that. To view it please enter your password below:" ) . '
	<label for="' . $label . '">' . __( "Password:" ) . ' </label><input name="post_password" id="' . $label . '" type="password" size="20" /><input type="submit" name="Submit" value="' . esc_attr__( "Submit" ) . '" />
	</form>
	';
	return $o;
}
?>

To edit the text for the password intro simply modify the line of text you see above to suit your needs. You can tweek this function as much as you want to suit your styping needs. Add headings, images and more to bring that extra customization to WordPress and set your blog apart from the rest.

Remember, don’t edit WordPress’ core files

You may have stumbled upon a solution from Google that suggest modifying /wp-includes/post-template.php and this is a bad idea. Please disregard this advice as it’s always in your best interests to not modify the WP core.

Help I’m getting a Fatal Error

OMG you blew up my WordPress! It’s going to be OK. Most likely there’s simply an extra PHP end-tag or one missing. Double-check that there’s no syntax errors in your functions.php file.

A special thanks to Michael Fields who I got this useful filter from over at WordPress.org.

Related posts:

  1. CSS Negative Text Indent Property: Looks Spammy and Bad for SEO
  2. WordPress 3.0 Custom Post Type Plugins
  3. How to Get the WordPress Parent Page Permalink
  4. The Best WordPress 3.0 Custom Post Types Tutorials
  5. Awesome Way to Add Dynamic Facebook and Tweet Widgets
TwitterDiggDeliciousStumbleuponTechnoratiFacebook