Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

User:Hearvox/Password Protected Posts

This article is a ROUGH DRAFT. The author is still working on this document, so please do not edit this without the author's permission. The content within this article may not yet be verified or valid. This information is subject to change.

(Notes for a Codex page on Password Protected Posts)

You can set the Content Visibility of your WordPress posts (and Pages) with the Visibility setting. By default, Visibility is "Public," making the content of a post with a Published status publicly visible.

The options, found in the Publish box of the Edit Post Screen, are: "Public," "Password protected," or "Private."

Checking "Private" makes your post visible only to your Users with the role of Administrator or Editor. 

Checking "Password protected,"

reveals a form field for you to enter a password.

You can password-protect your WordPress post or Page by setting its Content Visibility. By default, all the content of posts and Pages with a Published status is publicly visible.

<img class="alignright" src="http://codex.wordpress.org/images/6/6f/password_visibility.jpg" alt="Post Edit- Publish and Visibility box" width="309" height="361" />Password-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 Content Visibility of a post or Page to: "Password-protected." changes the:

  1. Title– Adds this string "Protected: ", to the output of <a title="post-template.php in tags/3.1.1/wp-includes – WordPress Trac" href="http://core.trac.wordpress.org/browser/tags/3.1.1/wp-includes/post-template.php#L91">get_the_title()</a>.
  2. Excerpt– Returns this text when we <a title="post-template.php in tags/3.1.1/wp-includes – WordPress Trac" href="http://core.trac.wordpress.org/browser/tags/3.1.1/wp-includes/post-template.php#L253">get_the_excerpt()</a>: "There is no excerpt because this is a protected post."
  3. Content– Returns a password form (w/ text) when we <a title="post-template.php in tags/3.1.1/wp-includes – WordPress Trac" href="http://core.trac.wordpress.org/browser/tags/3.1.1/wp-includes/post-template.php#L172">get_the_content()</a>.

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: <label for="pwbox-2923">Password:</label> <input id="pwbox-2923" name="post_password" size="20" type="password" /> <input disabled="disabled" name="Submit" type="submit" value="Submit" />

(Custom Field content can still display .)

Hide Custom Fields in a Password-protected Page/Post

This one function: <a href="http://codex.wordpress.org/Function_Reference/post_password_required" title="Function Reference/post password required « WordPress Codex">post password required()</a>, 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: [php]if ( ! post_password_required() ) { // Code to fetch and print CFs }[/php]

Change the Password-protected Single Page/Post Text

WP's logo may be: <img src="http://s.wordpress.org/style/images/codeispoetry.png" alt="Code is Poetry" width="133" height="9" />, but those hard-coded Password messages are anything but poetic. As always, though, WP <a title="Plugin API/Hooks « WordPress Codex" href="http://codex.wordpress.org/Plugin_API/Hooks">Hooks</a> us up with the <a title="Plugin API/Filter Reference « WordPress Codex" href="http://codex.wordpress.org/Plugin_API/Filter_Reference">filters</a> to customize that text.

Someone in the support forums asked: <a title="WordPress › Support » How do I change Password Protected text?" href="http://wordpress.org/support/topic/how-do-i-change-password-protected-text#post-1255215">How do I change Password Protected text?</a>

Plugin developer <a href="http://wordpress.mfields.org/" title="Michael Fields">Michael Fields</a> 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 htmlscript="true" highlight="7"]<?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; } ?>[/php]

Change the Password-protected Excerpt Text

This <a title="Showing the post password form for excerpts in WordPress" href="http://justintadlock.com/archives/2009/06/24/showing-the-post-password-form-for-excerpts-in-wordpress#comment-136641">comment</a>, by J Mehmett in a Justin Tadlock tutorial, show us how to replace the default excerpt text — HTML allowed:

[php htmlscript="true" highlight="4"]<?php function excerpt_protected( $excerpt ) { if ( post_password_required() ) $excerpt = '[This is password-protected.]';

return $excerpt; }

add_filter( 'the_excerpt', 'excerpt_protected' ); ?>[/php]

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 <a title="Showing the post password form for excerpts in WordPress" href="http://justintadlock.com/archives/2009/06/24/showing-the-post-password-form-for-excerpts-in-wordpress">"Show the post password form for excerpts</a>" by calling the core function <a title="post-template.php in tags/3.1.1/wp-includes – WordPress Trac" href="http://core.trac.wordpress.org/browser/tags/3.1.1/wp-includes/post-template.php#L1201">get_the_password_form()</a>.

Now go protect some Posts with personally preferable replacement prose.

"Password goes here" Edit Post or the Quick Edit screens