I haven’t posted in a very long time, but as soon as I stumbled on this functionality I had to share it immediately, so here it is:
It is possible to add a filter function to add new text to the password protected page. The code to add to your functions.php files is as follows:
<?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 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;
}
?>
…and an if statement, you can add a check to see if a custom field exists, and if it does, to spit it out instead of using the default text. Added code is in Yellow.
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">';
if (get_post_meta($post->ID, 'password-prompt-text', true)) {
$o .= get_post_meta($post->ID, 'password-prompt-text', true);
} else {
$o .='<p>'. __( "This post is password protected. To view it please enter your password below:" ).'</p>';
}
$o .= '<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;
}
The variable $o begins return a string. My if statement in yellow, interrupts the string with an if statement to see if the post has a custom field called password-prompt-text, and if it does, uses it instead of the text “This post is password protected…”.
Using this method, you can provide HTML into the description, so go crazy. I used H2 for title, then a paragraph for description. The cool thing about this, is that because of the else: it degrades nicely if you don’t have the custom field added to the post.
Hope that helps somebody.
Credit to Michael Fields for the original code, Googled and found here. Also mentioned at WordImpressed.
September 9, 2011
T says:
You should add a sleep(1); or sleep(2); in the loop that inserts each post. As it stands, when it mass imports the posts, the times are in microseconds between each post. This messes up my “previous link” and “next link” as they are arranged by alphabetical and as well as date/time since differences in microseconds between the posts can’t be differentiated very well and results in randomized order of all posts added.