Hack: Change The Password Protected Text

When you password-protect a post, you get a standard message of “This content is password protected. To view it please enter your password below:” that is difficult to change. But what if you want to do some simple members-only posts every once in a while and send out the password for special posts every week in your newsletter. It could be a nice way to encourage people to sign up for your emails without needing to install a whole membership plugin. You could even use fun words that relate to your topic as passwords.

Some suggest adding a whole plugin to change this text. But you know how I feel about installing plugins when it isn’t really needed. Others will use a functions.php hook and rewrite the entire content of the password form. But is that really necessary? Seems like it is easier to just replace the text and let the rest of the form stand as-is. So here is a function that will do just that.

add_filter('the_content', 'wpfilter_password_protected_message');
function wpfilter_password_protected_message($content) {
	global $post;
	if (!empty($post->post_password)) {
		$content = str_replace('This content is password protected. To view it please enter your password below:','Please enter the current members-only password from the newsletter to view this post.',$content);
	return $content;

I suppose the only danger is if the standard password text changes in some future version of WordPress. But that is easy enough to spot if it happens. I think at one point it said “post” instead of “contact,” as an example.

Notice I don’t even bother to check to see if the person has already entered the password. Since it just replaces the text that appears otherwise, it doesn’t matter. You could also only replace a portion of the text. So maybe replace just the second sentence, for instance.

Leave a Reply

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