Welcome to Dagon Design. In addition to free scripts, WordPress plugins, and articles, we offer a variety of services including custom theme design, plugin creation, and PHP scripting. Contact me for more information.

Updated Sunday, February 22nd, 2009 at 8:20pm

Show the Intro to Password Protected Posts in WordPress

This is a small hack for WordPress that allows you to show the first part of password protected posts. It will display the content you have before the ‘more’ tag, followed by the standard password box. I wrote this up for a member of the WordPress forum, and I thought I would share it here. Update: Instructions are now provided for WordPress 2.2

Before you begin

The instructions below have been verified in WordPress versions 2.0 through 2.8. The only difference between these versions is the file you have to edit.

Instructions

For WordPress 2.0 or 2.1, open /wp-includes/template-functions-post.php

For WordPress 2.2 through 2.8+, open /wp-includes/post-template.php

Find the following text:

$output = get_the_password_form();
return $output;

Replace it with this:

$content = $pages[$page-1];
$content = explode('<!--more-->', $content, 2);
$output = "";
$output .= $content[0];
$output .= get_the_password_form();
return $output;

This next step is optional, but I think it makes it nicer.

In the get_the_password_form function, find the following text (may vary between versions):

This post is password protected.

And replace it with this:

The rest of this post is password protected.

Or whatever text you would like it to show.

  If you have found this page useful, please consider donating. Thanks!

  1. 38
    Nick

    hey, i don’t know how to use the file and everything. i tried putting it into the url and it doesn’t show me the page. could you email me soon? thanks for your help! (:

  2. 37
    Mari

    i’m confused, what’s this file we’re supposed to open? i’ve tried adding the /wp-includes/post-template.php to the url but that doesn’t work. could you drop me an email at russmsoh@gmail.com please? thanks! :)

    not a very good computer person if you haven’t noticed. :D

  3. We supply all kinds of moncler boots,moncler bags,moncler coats,moncler jackets,moncler T-shirt,moncler vest and so on.We have got a good reputation pf our products with top quality and good price.We sincerely look forward to futher cooperation with you for mutual benefits.We are sure that you will find working with us is a pleasant, time-saving and profitable experience. Please feel free to contact us if you have any questions.Our website is http://www.newlyapparel.com.

  4. thank u so much!

  5. I am able to get it to work with WP 2.92, please send me an email or leave your email I can send you the modified file.

  6. Is there a way to do this in WP 2.9.x? I just tried and couldn’t find the file containing that bit of code. I would love to get this working.

    Thanks.

  7. Great post, will do this asap. Thank you for the tip.

    Regards,
    Peter Trape

  8. I’ve tried the combinations above and ended up with this
    $returns = mysql_query($query);
    $result = mysql_fetch_row($returns);
    $post_title = $result[5];
    $post_excerpt = $result[7];
    It worked for a while and now it only displays the title twice :(
    Any help please?
    Andrew

  9. We have updated the “better-protected-pages” plugin to V1.1 (available from wordpress.org) which works even better and allows you to show an excerpt before the improved password area. The best thing is that there is also a “Re-lock” button added, which allows you to lock the page again when you have finished reading it.

  10. Sorry just missed to put the “;”

    
    $output = $post->post_excerpt;
    

  11. There is a better way to custom this and be able to show your custom excerpt of your post.

    On Wordpress 2.8.3 I did this
    Update then your post-template

    Original code

    
    function get_the_excerpt($deprecated = '') {
    	global $post;
    	$output = '';
    	$output = $post->post_excerpt;
    	if ( post_password_required($post) ) {
    		$output = __('There is no excerpt because this is a protected post.');
    		return $output;
    

    You have to change the following line :

    
    $output = __('There is no excerpt because this is a protected post.');
    

    by

    
    $output = $post->post_excerpt
    

    I did it and it’s working great ;)

  12. Hi Everyone,

    Just wanted to say that I have just created a plugin which does this… and more! You can get it from my website, or direct from the wordpress plugins section – search for “better-protected-pages”

    Hope you like it.

    Tim

  13. 26
    lenny

    A tutorial or some help would be nice. I’m not sure where or how to use this “/wp-includes/post-template.php” any help would be appreciated.

  14. Is it possible to show different content if you do not have the password entered, and if you enter the password, you get totally, different content?

  15. This code has been tested, and works, with the latest WordPress release – 2.8

  16. useful hack trick, thanks.

    BTW, can I use this hack on blogs hosted on wordpress.com?

  17. it will display the content you have before the ‘more’ tag …

    what hack do you do to display ‘an excerpt’ of the first 30 words?

  18. This code modification has been tested in the latest release of WordPress (2.7.1)

  19. Thank you for this trick !!
    I’ve used it on a Wp 2.7 blog, and it works!
    Bye,
    Massimo

  20. 19
    whoopeedo

    Hey there,

    Instructions

    For WordPress 2.0 or 2.1, open /wp-includes/template-functions-post.php

    For WordPress 2.2 or 2.3, open /wp-includes/post-template.php

    it says to open those templates, I’m sorry but I’m an ameteur at this, how do I open them ?

  21. 18
    Willy

    Is this hack still works with release 2.7 Wordpress? THanks

  22. 17
    capdase

    how can i make it work? :( (

  23. Wonderful hack! I’m using it. Thank you.

  24. Works perfectly, great tutorial. Much help.

  25. I added a solution for this to the WordPress forums. It uses the same basic approach as jam (comment #13), but it replaces the message with the post’s excerpt. This also solves the issue raised by jalansutera (comment #1) and it does work on pages (comment #10).

    http://wordpress.org/support/topic/169520

  26. This is a much better solutions, without breaking in to core code:

    function change($title)
    {
    $vardi= array(“Å is raksts ir aizsargāts ar paroli. Lai to lasÄ«tu, lÅ«dzu ievadiet paroli:”, “Protected with password”);

    return str_replace($vardi,’LÅ«dzu, ievadiet paroli, lai Å¡o sadaļu apskatÄ«tu!’, $title);
    }

    add_filter(‘the_content’,'change’);

    if (have_posts()) : ?>

  27. 12
    mellow

    hi, i’m using wordpress 2.3.2 and I can’t seem to find this:

    $output = get_the_password_form();
    return $output;

    Please advise, thanks!

  28. Notice: The instructions above have been tested with WordPress 2.3, and the same method works. The documentation has been updated to reflect this.

    Michael: Yes, it will work for pages – I just tested it.

  29. Hi there,

    Nice code you got here. Everything above is about having a intro before password in Post.
    I was wondering if the same will work for Pages. I would like to have a intro on my page followed by the password box.

    I am WP 2.2, so hope you can help.

    Thanks

    Michael

  30. you’re amazing!!!! (:

  31. Wesley: Thanks, fixing now.

  32. Everything work except that you reversed the file names.

    For WordPress 2.0, open /wp-includes/template-functions-post.php

    For WordPress 2.2, open /wp-includes/post-template.php

    Again thanks for the hack.

  33. Wesley: I took a look at the files, and the instructions are the same for WordPress 2.2

    The only difference is the file you edit – which is listed above.

  34. 5
    Wesley

    Thanks, I am looking forward to your instruction for the 2.2

  35. Wesley: Since the template-functions-post.php file does not even exist in WP 2.1, no. I plan to eventually post updated instructions for 2.1 though.

  36. Is this mod work with the current release WP 2.2.1?

    thanks

    Wesley

  37. jalansutera: Unfortunately not. I have not done much work with modifications to the feed generation system yet.

  38. Hi.. I like your hack. I have put it on my blog too.

    However, the feed of the protected entry shows ” There is no excerpt because this is a protected post”. Do you know how to make the excerpt of the post appears? Do you know how to do it?

    Please help…

Leave a Comment

Before you comment: If you are having an issue with a script, please make sure you have read the entire article. Also, please read through the comments because most common issues have already been discussed many times. Thanks.


Be sure to wrap all code in <code></code> tags.