I recently came across an interesting situation that I had never thought about before. 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 theme’s functions.php file and drop this code in:
<?php
add_filter( 'the_excerpt', 'excerpt_protected' );
function excerpt_protected( $content ) {
if ( post_password_required() )
$content = get_the_password_form();
return $content;
}
?>
This will give us a new message:
This post is password protected. To view it please enter your password below:
Followed by that message is an input field for the post password.
Writing shorter tutorials
For those of you that follow this blog regularly, you know that I typically write long, in-depth tutorials. Many times, these are advanced techniques that not everyone will use. My plan is to start mixing in some quick tutorials that only take a few minutes to implement and maybe help out the average user more. This way, I can write more often. The longer tutorials tend to take a few days of work.
If you have any ideas for WordPress or bbPress tutorials, feel free to let me know in the comments or through my contact form.


That’s really awesome. Short and sweet!
[...] Show the password on post excerptsBy Austin on June 24, 2009 Hello there! If you are new here, you might want to subscribe to the RSS feed for updates on this topic.Justin Tadlock has written a nice tutorial on how to show a password form field instead of the defau… [...]
Nice little trick Justin:)
Nice tip Justin. Will the visitor be automatically redirected to the single post or will they be returned back to the page where they entered the password?
The Frosty — That’s the way I like to do it sometimes.
Adam W. Warner — Thanks. Maybe it’ll come in handy one day.
johnbillion — The visitor will be returned to the page in which they entered the password. The difference will be that they’ll be able to read the post’s excerpt.
Hi, Justin. I had a comment on this topic with experimental code on it. It seems as your spam filter ate it again
Hey Justin, is this code referred to WordPress 2.7? I ask you because in 2.8 it works without editing functions.php… Thanks in advance!
J Mehmett — Sorry. Your comment is long gone now. You can try posting it again if you want.
Danny — Nope, this was all done on WordPress 2.8. I’m sure it’s probably the same on 2.7. Remember, this deals with excerpts only (just in case you are using the full post).
Justin, I don’t know, why its not save to post a code in the comments area. It goes to the moderation queue or it gets eaten by Akismet
Back to the comment, it was nothing but a possibility of replacing the default text with a friendly one in the excerpt instead of showing the login form.
The above code replaces the default:
…with:
I’m sorry Justin, I have misunderstood your post!
This is exacly watch I was watching to do in my blog,I was relly neeed of this feature!
Thanks a lot and congratulations for your excellent blog!
I don’t suppose anyone knows how to allow the excerpt to show on a protected post? Like a teazer…
[...] 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 [...]