WP Password Prose

Post Edit- Publish and Visibility boxPassword-protecting a WordPress Post/Page hides its Content and Excerpt, but not its Custom Fields: those can still show. Below are ways to hide ’em, and functions for customizing the default WP Password-Protected messages.

Password-protect a Page/Post

Setting the Visibility to Password-protected changes the:

  1. Title– Adds this string "Protected: ", to the output of get_the_title().
  2. Excerpt– Returns this text when we get_the_excerpt(): “There is no excerpt because this is a protected post.”
  3. Content– Returns a password form (w/ text) when we get_the_content().

So in places that Post/Page lists its Title & Excerpt (e.g., Index, Category, Search views), we see:

Protected: Post’s or Page’s Title
There is no excerpt because this is a protected post.

And the password-protected single Post/Page itself looks like:

Protected: Post/Page Title
This post is password protected. To view it please enter your password below:

(Custom Field content can still display .)

Hide Custom Fields in a Password-protected Page/Post

This one function: post password required(), checks both “whether post requires password and correct password has been provided.” So, in single.php, just wrap a big-ass if around all your get_post_meta()s, and CFs shan’t show:

if ( ! post_password_required() ) {
	// Code to fetch and print CFs
}

Change the Password-protected Single Page/Post Text

WP’s logo may be: Code is Poetry, but those hard-coded Password messages are anything but poetic. As always, though, WP Hooks us up with the filters to customize that text.

Someone in the support forums asked: How do I change Password Protected text?

Plugin developer Michael Fields answered:

“This piece of code should do it for you. Place this code in your theme’s functions.php file. You can add customizations to the custom_password_form() function – just don’t use print or echo – the function must return a value.”

<?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. 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;
}
?>

Change the Password-protected Excerpt Text

This comment, by J Mehmett in a Justin Tadlock tutorial, show us how to replace the default excerpt text — HTML allowed:

<?php
function excerpt_protected( $excerpt ) {
	if ( post_password_required() )
		$excerpt = '<em>[This is password-protected.]</em>';

	return $excerpt;
}

add_filter( 'the_excerpt', 'excerpt_protected' );
?>

The above goes in your functions.php file. Put your password-protected excerpt-text where mine sez: [This is password-protected.].

Put the Password-protected Form in the Excerpt

Lastly, JT’s tut also shows us how to “Show the post password form for excerpts” by calling the core function get_the_password_form().

Now go protect some Posts with personally preferable replacement prose.

Bookmark and Share
« | HV NEWS | »




Leave a comment:

(required)

(required) (will be hidden)


(Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> )