the_excerpt() or get_the_excerpt with shortcode escape

You can make WordPress execute your shortcode in the excerpt by hooking into get_the_excerpt filter and overwriting the default wp_trim_excerpt function, which is responsible of stripping the shortcode tags from the excerpt as pointed by Chip:

add_filter('get_the_excerpt', 'do_my_shortcode_in_excerpt');
function do_my_shortcode_in_excerpt($excerpt) {
    return do_shortcode(wp_trim_words(get_the_content(), 55));
}

This is applied to both the_excerpt() and get_the_excerpt() outputs. If you want to apply it only for the_excerpt() output, hook to the_excerpt filter:

add_filter('the_excerpt', 'do_my_shortcode_in_excerpt');
function do_my_shortcode_in_excerpt($excerpt) {
    return do_shortcode(wp_trim_words(get_the_content(), 55));
}

Leave a Comment