1

I'm looking to modify the output of the_excerpt when there isn't an excerpt specified and the post is password protected. I already can modify the password protected form to whatever I want, but the_excerpt is outputting "There is no excerpt because this is a protected post.".

The code for this lies in wp-includes/post-template.php:

function get_the_excerpt( $deprecated = '' ) {
    if ( !empty( $deprecated ) )
        _deprecated_argument( __FUNCTION__, '2.3' );

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

    return apply_filters('get_the_excerpt', $output);
}

An easy solution would be to simply change the code in there and move on, but as well all know, changing core files deserves more than a slap across the face! Can anyone help me figure out how to modify this code in my functions.php file?

Thanks!

EDIT: Here's the code I tried out, but it isn't working :(

add_filter('get_the_excerpt', 'improved_get_the_excerpt');
function improved_get_the_excerpt( $deprecated = '' ) {
    if ( !empty( $deprecated ) )
        _deprecated_argument( __FUNCTION__, '2.3' );

    global $post;
    $output = $post->post_excerpt;
    if ( post_password_required($post) ) {
        $output = __('This is some test content.');
        return $output;
    }
}

I'm completely new to the concept of filters, actions, hooks, etc so please bear with me! I appreciate the help!

3 Answers 3

2

Simple way would be to check output in template (or small wrapper):

$excerpt = get_the_excerpt();

if( 'There is no excerpt because this is a protected post.' == $excerpt ) {

    // stuff
}
else {

    echo $excerpt;
}

Another way would be to use gettext filter (which is used inside __()) to catch and modify that phrase.

1
  • +1 for the method I tried first! I had almost the exact same code when I first tried to tackle this problem, but it doesn't seem like the most efficient way. I'm actually trying to do it the way I asked in my question so that I am forced to learn something new about WordPress :) specifically actions, filters, hooks, and general plugin development processes.
    – cmegown
    Commented Mar 17, 2012 at 20:51
1

You'd want to look into add_filter().

Here's the codex link to it: http://codex.wordpress.org/Function_Reference/add_filter

Here's another Article I refer to from time to time: http://otto42.com/ak

-Cheers

Edit: Passing a parameter to filter and action functions

1
  • +1 for pointing me to the API, which I hadn't come across. On a related note, doesn't it seem shockingly incomplete? Also, please see my edit - I still can't quite get it to work!
    – cmegown
    Commented Mar 17, 2012 at 20:49
1

get_the_excerpt() is not a pluggable function, so you cannot overwrite it by simply naming a function the same as it. Because there is no hook between checking the protected status and returning the text that the excerpt cannot be displayed, you will have to do the filtering once the_excerpt() has been gotten using a method as suggested by @Rarst in his solution.

There is a (hacky) way to get around this, you can use the i18n features to replace the text that is output by the function, but this is a bad solution as it uses the i18n features in a way that they are wholly not intended to be used.

1
  • +1 Thanks for the advice, I learn something new from this site every day!
    – cmegown
    Commented Mar 18, 2012 at 0:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.