the_excerpt – want to modify password protected output

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:

Read More
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!

Related posts

Leave a Reply

3 comments

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

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