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!
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…
Just what I needed, thanks!
Thanks for sharing Justin.
This is just what I need!!
I will try this on my blog.
Is there a better way to do this now with v3.3.1? I tried this, and it ends up spitting out two password forms: one that is generated by the code above, and one that is generated by default. And yes, I am using the_excerpt.
Thanks, and I hope to hear from you folks soon.
I added this codes in function.php and it works perfect. Thanks!
I want to output the password protect form on a single page or post, so that when I enter a specific password into the form, it takes me to any post or page having such password. All I need is just a single page to output my password form irrespective of the post, you can reach any password protected post or page by entering the right password for such post or page. I just hope this is quite explanatory enough.