Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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!

share|improve this question
add comment

3 Answers

up vote 2 down vote accepted

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.

share|improve this answer
    
+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 Mar 17 '12 at 20:51
add comment

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

share|improve this answer
    
+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 Mar 17 '12 at 20:49
add comment

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.

share|improve this answer
    
+1 Thanks for the advice, I learn something new from this site every day! –  cmegown Mar 18 '12 at 0:30
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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