Search code examples
wordpresswordpress-hook

WordPress hook for changing excerpt content when excerpt not set


Title says pretty much all of it. I need a filter that I can use to change excerpt content for those posts that have no excerpt set.

Now, I've tried both the_excerpt and get_the_excerpt, they both pass one parameter, and in both cases said parameter is empty string.

That said, I will need to hook onto a filter that has access to the auto-generated except, and lets me change it.


Solution

  • Perhaps something like this (untested)?

    function my_filter_the_excerpt( $excerpt ) {
    
        global $post;
    
        if( empty( $post->post_excerpt ) ) {
            // Excerpt is auto-generated
            $excerpt = 'Something else...';
        }
        return $excerpt;
    
    }
    add_filter( 'get_the_excerpt', 'my_filter_the_excerpt' );