The get_the_excerpt filter is used to filter the excerpt of the post after it is retrieved from the database and before it is returned from the get_the_excerpt() function.
When the get_the_excerpt filter is called, the filter function is passed a single argument containing the post excerpt.
Where ‘filter_function_name‘ is the function WordPress should call when the excerpt is being retrieved. Note that the filter function must return the excerpt after it is finished processing, or page sections showing an excerpt will be blank, and other plugins also filtering the excerpt may generate errors.
The ‘filter_function_name‘ should be a unique function name. It cannot match any other function name already declared.
When using this filter on a string stored in a variable, the string will not be trimmed by the excerpt_length filter.
This is expected behaviour. By passing a long string through the get_the_excerpt filter, you are simulating the case where you have entered the same long text into the Excerpt field in the Edit Post screen. When a custom excerpt is present on the post, no trimming is done (though other content filters are applied).
If you want to simulate the automatic excerpt trimming on arbitrary text, you can pass the text to wp_trim_words() yourself, with a function like this:
function wpcodex_format_custom_excerpt( $text ) {
/**
* Filters the number of words in an excerpt.
*
* @since 2.7.0
*
* @param int $number The number of words. Default 55.
*/
$excerpt_length = apply_filters( 'excerpt_length', 55 );
/**
* Filters the string in the "more" link displayed after a trimmed excerpt.
*
* @since 2.9.0
*
* @param string $more_string The string shown within the more link.
*/
$excerpt_more = apply_filters( 'excerpt_more', ' ' . '[…]' );
$text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
// Format the text
$text = apply_filters( 'get_the_excerpt', $text );
return $text;
}
The function above can be used like this:
$text = "Etiam laoreet libero sit amet sem tempor, vel dictum odio bibendum. Aenean odio ligula, placerat sodales dui non, tempor dictum lorem. Vestibulum rutrum, velit a placerat imperdiet, erat massa porta urna, a convallis diam lorem non sapien. Vivamus in risus non quam aliquet blandit nec fermentum dolor. Duis ultricies lectus eu cursus fermentum. Sed eget convallis odio. Ut ut dolor nec nisi varius blandit a eget justo. Integer sed tellus eget leo pretium ultricies. Nullam rhoncus ex sit amet dolor pellentesque feugiat. Nullam a eros orci. Etiam egestas est erat, eu pellentesque sapien dignissim vel. Nulla malesuada commodo justo, at egestas purus egestas fermentum.
Vestibulum vitae metus ullamcorper, vehicula urna eu, ullamcorper leo. Sed sit amet eros eget metus bibendum blandit. Praesent ac lacinia purus. Donec laoreet tempus dui id faucibus. Nulla laoreet cursus laoreet. Nulla et arcu ex. Pellentesque cursus non metus a volutpat. Donec pretium orci et metus molestie, ac feugiat lacus auctor. Ut vehicula eu augue eu gravida. In scelerisque risus in rutrum vestibulum. Phasellus egestas augue quis enim varius, ut sagittis massa auctor.";
$text = wpcodex_format_custom_excerpt( $text );
This example from the twentyeleven theme appends a custom “Read more” link to post excerpts. See has_excerpt() and is_attachment().
/**
* Adds a pretty "Continue Reading" link to custom post excerpts.
*
* To override this link in a child theme, remove the filter and add your own
* function tied to the get_the_excerpt filter hook.
*/
function twentyeleven_custom_excerpt_more( $output ) {
if ( has_excerpt() && ! is_attachment() ) {
$output .= twentyeleven_continue_reading_link();
}
return $output;
}
add_filter( 'get_the_excerpt', 'twentyeleven_custom_excerpt_more' );
When using this filter on a string stored in a variable, the string will not be trimmed by the
excerpt_length
filter.This is expected behaviour. By passing a long string through the
get_the_excerpt
filter, you are simulating the case where you have entered the same long text into theExcerpt
field in theEdit Post
screen. When a custom excerpt is present on the post, no trimming is done (though other content filters are applied).If you want to simulate the automatic excerpt trimming on arbitrary text, you can pass the text to
wp_trim_words()
yourself, with a function like this:The function above can be used like this:
Examples migrated from Codex:
Custom “More” Link
This example from the twentyeleven theme appends a custom “Read more” link to post excerpts. See has_excerpt() and is_attachment().
Fallback excerpt for posts with AUTOMATED excerpt: