In the past, we have shown you how to add and use widgets in WordPress. One of the most asked questions we get about widgets is how can I add shortcodes in WordPress sidebar widgets? By default, shortcodes does not work in sidebar widgets. In this article, we will show you how to easily use shortcodes in your WordPress sidebar widgets.
Why Shortcodes Doesn’t Work in WordPress Widgets?
Often beginners think that because there’s a text widget in your sidebar, shortcodes will automatically work. By default, any text entered into the text widget goes through WordPress filters which doesn’t allow shortcodes to be executed.
Instead of seeing the final result, you will see the shortcode itself as plain text.
It is very easy to change that, and we will show you how to easily enable shortcodes to be executed in WordPress text widgets.
Method 1: Adding Shortcode Enabled Text Widget in WordPress
In this method, we will be using a simple WordPress plugin. This method is recommended for beginners who don’t want to add code manually.
Upon activation, you need to visit Appearance » Widgets page. There you will find a shortcode widget in the list of available widgets.
Simply add the shortcode widget to a sidebar and then add your shortcode in the content box. Don’t forget to click on the save button to store your widget settings.
This shortcode widget is just like the default text widget in WordPress. The only difference is that you can add shortcodes inside it.
Method 2: Enabling Shortcodes in WordPress Text Widget
If you are comfortable adding code to your theme, then you can enable shortcodes to work in text widgets.
// Enable shortcodes in text widgets add_filter('widget_text','do_shortcode');
This code simply adds a new filter allowing shortcodes to run inside text widget.
You can now visit Appearance » Widgets page and add a text widget to a sidebar. Paste the shortcode in the content box and click on the save button.
Visit your website to see shortcode working inside text widgets.
We hope this article helped you learn how to use shortcodes in your WordPress sidebar widgets. You may also want to see our list of 7 essential tips for using shortcodes in WordPress.