WordPress: How to use shortcodes in WordPress sidebar widgets

Shortcodes make it easy to blog and add various elements in WordPress. When using the shortcode, say, my_shortcode, you just need to add [my_shortcode] (with brackets) in the content at the desired location. However, just adding the shortcode in sidebar widgets does not execute it and you get to see the text of the shortcode. How to add a shortcode in WordPress sidebar widgets?

With a small code, you can instruct WordPress to execute shortcodes in sidebars as well. Open your theme’s functions.php file and paste the following code.

add_filter('widget_text', 'do_shortcode');

Once you add the above code snippet to functions.php, you can add shortcodes in WordPress sidebar like posts. Go to your Appearance → Widgets screen and create a text widget. Paste any shortcode that you have enabled in the sidebar widget, and it will function properly.