Showing the post password form for excerpts in WordPress

June 27, 2009 · Print This Article

Many WordPress themes show excerpts on the front page and other archive-type views. When viewing a password-protected post, this message is displayed:

There is no excerpt because this is a protected post.

While this is fine for many scenarios, it may not be the most helpful piece of text for the average Internet user. Instead of showing the message, we’ll replace it with a password form.

Replacing the no excerpt text with a password form WordPress comes packaged with a neat function called get_the_password_form() that handles most of the work for us. It generates the entire form. All we need to do is load it at the appropriate time.

Open your wordpress theme’s functions.php file and paste following code:

<?php

add_filter( 'the_excerpt', 'excerpt_protected' );

function excerpt_protected( $content ) {
if ( post_password_required() )
$content = get_the_password_form();

return $content;
}

?>

Then this will give us a new message:

This post is password protected. To view it please enter your password below:

Now followed by that message is an input field for the post password.


Your choice for site templates and wordpress themes

Comments

Got something to say?

You must be logged in to post a comment.