Shortcodes are good to use when you want to add additional functionality to text areas. Those shortcodes used in the post editor will show the functionality automatically when you preview the post. However, if you want to add the same functionality easily to WordPress sidebar with a text widget, you would meet a trouble.
By default, shortcodes don’t work in text widgets because the built-in WordPress filters are not allowing them to be executed. So if you add a shortcode to a text widget, you will not see the functionality, and instead, the shortcode is displayed on the frontend of your WordPress site as plain texts.
Of course, there are solutions to this problem. In below, we will introduce two methods that you can utilize to enable shortcodes in WordPress text widgets. With either method, you can expect your shortcodes to function normally in the sidebar.
Method 1: Add a Shortcode Widget with a Plugin
In fact, this method is not about adding the shortcode support to the default WordPress text widget. Instead, the plugin you are going to try adds a new text-like widget to your website which allows the use of shortcodes.
The new widget can be used in the same way as any other text widget except that it supports shortcodes. And it is currently the easiest way for those who are not comfortable with editing codes.
The plugin to use is named Shortcode Widget. Firstly, you have to install the plugin and activate it. Simple as Shortcode Widget is, there is no extra setting to deal with. So after the activation, you can look at the available widgets directly in the Appearance menu and find the new widget named “Shortcode Widget”.
To use your shortcodes in the sidebar, you only need to drag this new widget to the sidebar, and then add a shortcode in the content area. After entering a title for the widget, you can save it. If you don’t like the title to show, you can follow a simple tutorial to remove the widget title.
Now you can visit your WordPress site. Instead of seeing a line of plain text, you will notice that the functionality you want to add with the shortcode is already there in the sidebar.
Method 2: Allow Shortcodes by Adding a Filter
This method is about coding, but don’t worry, you only need to add a single line of code to the functions.php file of your theme, so no advanced skill is needed.
To be more specific, you have to make use of the following code which adds a filter that allows shortcodes to execute in the default WordPress text widgets.
As soon as this code is added to functions.php, you can test the new function by dragging a text widget to the sidebar and adding a shortcode to it. The shortcode should work alright now in the text widget.
Note: For easier theme customization and upgrade, we’d suggest you use the code in a child theme instead of the original theme. If you haven’t used any child theme, create one now.