Content & Posts PHP Beginner

Run Shortcodes in Excerpts

Make shortcodes typed into a post's Excerpt box work, instead of showing as raw [shortcode] text on archives.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 1 lines
add_filter( 'the_excerpt', 'do_shortcode', 11 );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.

If you type a shortcode into a post’s Excerpt box, WordPress prints it as plain text on blog and archive pages. This one line makes those shortcodes run.

How it works

the_excerpt filters the excerpt before it’s shown. Adding do_shortcode at priority 11, after WordPress’s own formatting, turns any shortcodes into their output.

Things to watch

  • This only affects excerpts you write by hand. Automatic excerpts remove shortcodes on purpose, before trimming the text.
  • Avoid shortcodes that output large blocks, like forms or galleries, in excerpts.

How to test it

Add [year] or another shortcode to a post’s Excerpt and view the blog page. You should see its output, not the shortcode.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only