By default, WordPress does not execute shortcodes in text widgets or excerpts. To insert a shortcode into a text widget and/or excerpt, you need to add some code into your functions.php:
//Enable Shortcodes in WordPress Excerpts and Text Widgets. Add to functions.php add_filter('the_excerpt', 'do_shortcode'); add_filter('widget_text', 'do_shortcode');
Make sure sure that you are using a Child Theme to add this code. If you aren’t, you’re code will be erased when you update your theme. Always use a child theme! ALWAYS!
Doug says
Thanks for sharing. Even though it’s 4 years after you posted, this answered exactly what I needed to get shortcodes working in our excerpts. Well, with a slight modification. Wanted to add that some themes use get_the_excerpt() to capture excerpt text instead of the_excerpt(). In that case, just add this line, and you’ll cover both cases:
add_filter(‘get_the_excerpt’, ‘do_shortcode’);
George says
Hi Ren,
I have struggled with getting this done. I am using a plugin that allows the use of shortcodes to pages and posts but I could not figure out how to get it done in the widget. I am eternally grateful for your short and precise code for its working like a charm 🙂 thanks a lot!
Jeff Yablon says
Ren, this is great, but begs a larger question: the insertion of shortcodes within shortcodes … wherever.
Some themes address this themselves … for themselves (THEIR shortcodes). Same with some shortcode collection plug-ins. But doing it universally … albeit within the environment you create for your theme … would be the real Holy Grail on this issue.
Your thoughts?
-Jeff
Ren says
Hi, Jeff. So you’re talking about nested shortcodes, basically? I know there are themes and plugins that use nested shortcodes and, in my experience, they typically serve as a substitution for writing HTML markup. As a developer, I cringe at the thought of needing to use shortcodes dependent on a theme/plugin just to avoid HTML. Instead of spending a ton of time learning how to use a theme’s specific shortcode system, I’d recommend that any user take that time to learn some simple HTML because it will get them farther in the long-run and make them less dependent on the theme.
Shortcodes certainly provide a benefit for end users and I don’t think they should be avoided completely but I do think they can be overused and, thus, easily force users to rely on third party tools just to structure basic content, such as columns, alert boxes, etc.
I wrote a couple posts to explain my views on shortcodes and WordPress theme-dependency. Here are the links if you’re interested:
http://www.engagewp.com/stop-using-theme-specific-wordpress-shortcodes/
http://www.engagewp.com/3-surefire-ways-avoid-theme-lock/