Shortcodes are stripped from the excerpt before filters are applied. Try something more like this:
function tsc_execute_shortcodes_in_excerpts( $text, $raw_text ){
if(empty($raw_text)){
$text = get_the_content('');
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
$text = strip_tags($text);
$excerpt_length = apply_filters('excerpt_length', 55);
$excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
$words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
if ( count($words) > $excerpt_length ) {
array_pop($words);
$text = implode(' ', $words);
$text = $text . $excerpt_more;
} else {
$text = implode(' ', $words);
}
}
return $text;
}
add_filter( 'wp_trim_excerpt', 'tsc_execute_shortcodes_in_excerpts', 10, 2 );
That basically re-runs wp_trim_excerpt()
on the content (minus shortcode stripping) if it was trimmed to begin with. If you want to target your shortcode only, you could do something like this after get_the_content('')
:
$text = str_replace( '[tsc', '{tsc', $text );
$text = strip_shortcodes( $text );
$text = str_replace( '{tsc', '[tsc', $text );
Hope that helps.