Modifying Wordpress Password Protected Pages With Teaser

I noticed that password protected pages / posts are still archived by Google, although the page is essentially ‘junk’ containing only a message saying that page is password protected and offering a password form to view it. I wanted to show a custom excerpt on the post / page as well as the password form, so it actually adds a little value to search engine indexing, so hopefully this has some SEO value. The other option was to look at adding a <meta name="robots" content="index,follow" /> to the header of pages that are protected (a good option – but in this case, I want the pages to be found).

As I am using the Hybrid theme framework, I didn’t want to go modifying the individual post and page templates to add the code to display the excerpt. I tracked down the code for adding a custom filter to modify the password protected text, and decided that this was the best way to be able to extract and display the custom excerpt as a ‘teaser’ when the page or post is viewed. The following code is added to the functions.php file in your Hybrid child theme.

/**
 * Display teaser on Password Protected Pages
 * Description: Outputs a custom excerpt on password protected pages and allows modification of the password form.
 * Author: Graeme Wilson
 * Author URI: http://graemewilson.co.nz/
 * Adapted from code by Michael Fields - http://profiles.wordpress.org/mfields/
 * Version:0.01
 */

add_filter( 'the_password_form', 'custom_password_form' );

function custom_password_form() {
	global $wp_query;
	$page_id = $wp_query->post->ID;
	$custom_excerpt = get_page( $page_id );
	$custom_output = '<div id="teaser"><p>' . $custom_excerpt->post_excerpt . '</p></div>' . "\r\n";

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

You can easily modify and adapt the code to add additional information before or after the password form. The teaser is in a <div> with the id of “teaser” so it can be easily modified by CSS, and of course you can adapt the HTML in the function to whatever you desire.

I thought of extending this function slightly to use custom fields to control whether the password protected page has a teaser or not. This is another option to explore.

add_filter( ‘the_password_form’, ‘custom_password_form’ );

function custom_password_form() {
global $wp_query;
$page_id = $wp_query->post->ID;
$custom_excerpt = get_page( $page_id );
$custom_output = ‘<div id=”teaser”><p>’ . $custom_excerpt->post_excerpt . ‘</p></div>’ . “\r\n”;

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

3 Comments


  1. Thanks for the code, helped me a lot.


  2. Do you know if this will work in a standard non-Hybrid theme? I’ve had the code from Michael Fields for a while, and just want to add in the excerpt part, but the excerpt is not displaying. I’m using WP3.3.1.


  3. Hi Shane, sorry for the late reply. Yes, it will work for non-Hybrid theme sites. It uses standard WordPress functions.

Leave a Reply