Shortcodes showing in excerpt despite using strip_shortcodes

On my website, shortcodes are showing in the excerpts, despite the fact that WordPress should remove them automatically, and that I tried this two different solutions:

function remove_shortcode_from_excerpt($content) {
    $content = strip_shortcodes( $content );
    return $content;
}
add_filter('the_excerpt', 'remove_shortcode_from_excerpt');

And

add_filter( 'the_excerpt', 'shortcode_unautop');
add_filter( 'the_excerpt', 'do_shortcode');

That I added in the functions.php file of my child theme.

What are my next options?

Topic shortcode excerpt php Wordpress

Category Web


So, as iguanarama pointed out, the first step is to make sure that the theme you are using uses the_excerpt() to show the excerpt.

You'll want to go check in your archive.php file how it is displayed. In my theme it was using the file 'template-parts/content-front-c.php'. Then go to this file and see what is the function displaying the excerpt. Right after the function gets the content, add $content=strip_shortcodes($content);, and, in my case, it worked !

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.