I think that the function you need is wp_trim_excerpt
. With this function you can generate a excerpt from any text:
$text = "Some context";
$excerpt = wp_trim_excerpt( $text );
It is important to note that wp_trim_excerpt
use the excerpt length set by WordPress, that is 55 words by default, and make use of excerpt_lenght
filter, so it will work with any plugin or theme that hook in this filter to define a custom excerpt length. If $text
is greater than the excerpt length, $text
is trimmed and ellips is appended. What to append can be modified using excerpt_more
filter.
You may also be interested in wp_trim_words
. This function uses also 55 words limit by default and it also append ellips by default. This function doesn’t use excerpt_lenght
filter nor excerpt_more
filter. To modify the lenght and what to append you have to pass that options directly to the function:
$text = "Some context";
$words = 55;
$more = "…";
$excerpt = wp_trim_words( $text, $words, $more );