Quick Tip: How to Use Shortcodes Within Widgets

Shortcodes has been in WordPress for quite some time. It allows developers to add more or less complex features in plugins and themes without user having to write single HTML code.

By default, shortcodes runs only in editor. If you want to use shortcodes in widgets, it’s not possible. But a single line of code will allow you to add shortcode in widget.

WordPress comes with plethora of widgets by default. One of such widget is “Text Widget”. This widget allows you to add any text and HTML code in it. To run shortcode in it, we will be using “widget_text” filter. This filter allows you to modify content of “Text Widget”. We will be using “do_shortcode” as a callback function in this filter. “do_shortcode” function renders shortcode for you.

The code below can be used in a theme functions.php file to run shortcode:

You can learn more about built-in shortcode in WordPress here.

You can get more details regarding shortcode API here.