I'm using the_excerpt() in my template loop to display post excerpt on the front page.

It's currently displaying unwanted shortcode directly on the front page


[box]post content[/box]

[alert]post content[/alert]

How can I filter out these shortcode only while keeping the actual content?

  • From where you are getting this unwanted shortcodes ? May 5, 2016 at 13:49
  • The shortcode was originally from another Wordpress framework. I'm now working on a customized theme. It's in the development stage, and I don't want to manually remove all the shortcode in hundreds of posts just to do the testing. When the_excerpt() is called, it displays the shortcode directly as plain text (since the new theme doesn't support the parsing of the shortcode).
    – KDX
    May 5, 2016 at 13:59

4 Answers 4


try this

add_filter( 'get_the_excerpt', 'strip_shortcodes', 20 );

or try this edit

echo strip_shortcodes( get_the_excerpt() );

if shortcode is not register with wordpress function add_shortcode

add_filter( 'the_excerpt', 'remove_shortcodes_in_excerpt', 20 );

function remove_shortcodes_in_excerpt( $content){
    $content = strip_shortcodes($content);
    $tagnames = array('box', 'alert');  // add shortcode tag name [box]content[/box] tagname = box
    $content = do_shortcodes_in_html_tags( $content, true, $tagnames );

    $pattern = get_shortcode_regex( $tagnames );
    $content = preg_replace_callback( "/$pattern/", 'strip_shortcode_tag', $content );
    return $content;
  • added this to functions.php and used echo get_the_excerpt() in my template, not working.
    – KDX
    May 5, 2016 at 16:10
  • edit answer try or condition May 5, 2016 at 16:16
  • The second edit doesn't work, the shortcode, eg. [box]the content[/box] is still there. Plus, it's changing the read more link into plain text.
    – KDX
    May 5, 2016 at 16:24
  • can you check backend edit post what is in excerpt field there. May 5, 2016 at 16:53
  • the excerpt field is empty. i let the system extract excerpt from the content, that's where the shortcode taking place.
    – KDX
    May 5, 2016 at 17:02
function wpsesess_ddecode_excerpt( $excerpt )
    return strip_shortcodes( $excerpt );
add_filter( 'the_excerpt', 'wpsesess_ddecode_excerpt' );

EDIT Can you please post this in functions.php and let me know?

  • Doesn't work, tried switch to echo get_the_excerpt() too. Could it be that after disabling the framework, the shortcode is no longer active/registered, thus the strip_shortcodes won't work? Is it possible to simply strip [anything]the content[/anything] whenever there's a pairing bracket of tags?
    – KDX
    May 5, 2016 at 14:32
  • Let's check the edited version May 5, 2016 at 14:42
  • The new version still doesn't work. Tried both the_excerpt() and echo get_the_excerpt()
    – KDX
    May 5, 2016 at 14:51
  • use this... add_filter( 'the_excerpt', 'shortcode_unautop'); add_filter( 'the_excerpt', 'do_shortcode'); May 5, 2016 at 14:58
  • Adding the two filters still doesn't help. I tried the two filters by themselves, and also tested them together with the function in your answer. I've also tried removing the_excerpt() and echo get_the_excerpt(), the post page is not showing anything, so the excerpt was properly called, just can't filter out the shortcode. Could it be any code execution priority issue?
    – KDX
    May 5, 2016 at 15:09


$excerpt = $post->post_excerpt;
$excerpt = apply_filters('the_content', $excerpt);
echo $excerpt;


$excerpt = $post->post_excerpt;
$excerpt = apply_filters('the_excerpt', $excerpt);
echo $excerpt;
  • I placed the code in my template file. The first one shows the exact same excerpt with shortcode in place. The second one gave me blank screen. I replaced the 3rd time of the first code to echo preg_replace('/\[box\]|\[\/box\]/', '', $excerpt); to manually strip off the box shortcode. Which does the trick but not a full solution. I have excerpt in my search result, category, tag, and latest post pages. I would like to have it done in functions.php so I can re-use it. Also need to apply to generic shortcode instead manually applying it.
    – KDX
    May 5, 2016 at 16:05

Since your new theme doesn't register the shortcodes they're regarded as plain text. Any attempt at filtering is useless, because as far as WordPress is concerned they aren't shortcodes.

The easiest way to solve this is to register the shortcodes in your new theme and hook them to an action that does nothing besides removing the shortcode.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.