The proper hooks for modifying the post excerpt are the ones you already tried: get_the_excerpt
and the_excerpt
, and WordPress actually uses the former one to generate an excerpt from the full post content, if there's no custom or manually-specified excerpt for the post — below is the relevant code in wp-includes/default-filters.php
:
add_filter( 'get_the_excerpt', 'wp_trim_excerpt', 10, 2 );
So if you want to access the auto-generated excerpt, then you can usewith the the_excerpt
hook, you can do it like so:
add_filter( 'the_excerpt', function ( $excerpt ) {
return has_excerpt() ?
'Has custom excerpt: ' . $excerpt :
'Automatic excerpt: ' . $excerpt;
} );
But take note, the automatic excerpt may not necessarily be the one initially generated by WordPress and WordPress might not even be the one that generated the excerpt — plugins could have completely overridden it or just customized it, just as you can do the same.
And as you may have guessed it, you can remove the default WordPress filter and then use your own callback to generate your own "automattic""automatic" excerpt:
remove_filter( 'get_the_excerpt', 'wp_trim_excerpt', 10, 2 );
add_filter( 'get_the_excerpt', function ( $excerpt, $post ) {
return $excerpt$post->post_excerpt ?
'Has custom excerpt: ' . $excerpt :
'Here, create your own excerpt.';
}, 10, 2 );