There are a number of WordPress themes and plugins that give you a shortcode to add content to a WordPress post or page, but they have no widget to use in a sidebar. This sometimes is necessary. For these reasons, in today’s article we will show you two easy ways to get shortcodes working in sidebar.
Adding shortcode in Text Widget
By default, any text entered into the text widget goes through WordPress filters which doesn’t allow shortcodes to be executed. This means that WordPress doesn’t execute shortcodes inside the default text widget. So you have to enable shortcodes to work in text widgets. For this you must to add the following code to your functions.php file of your child theme:
This code adds a new filter allowing shortcodes to run inside text widget.
Now, in your WordPress Dashboard, go to Appearance > Widgets and find Text widget among available widgets on the left. Drag it to the needed widget area and drop it there.
The last step is to paste the shortcode in the content box and to click on the save button.
Adding shortcode with a plugin
Firstly you must install and activate the Shortcode Widget plugin. Then, the procedure is simple: go to Appearance > Widgets and you’ll now see a new widget type called “Shortcode Widget”.
After that 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. To test the widget you can add the widget and use the shortcode “[shortcode_widget_test]”, it will display “It works” on the frontend and this will confirm the widget does work.
In fact, this shortcode widget is just like the default text widget in WordPress. The only difference is that you can add shortcodes inside it, without adding a few special filters.