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!

Pages: « 3 [2] 1 » Show All

  1. 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.

  2. Sorry just missed to put the “;”

    
    $output = $post->post_excerpt;
    

  3. 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 ;)

  4. 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

  5. 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.

  6. 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?

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

  8. useful hack trick, thanks.

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

  9. 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?

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

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

  12. 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 ?

  13. 18
    Willy

    Is this hack still works with release 2.7 Wordpress? THanks

  14. 17
    capdase

    how can i make it work? :( (

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

Pages: « 3 [2] 1 » Show All

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.