WordPress: How to use Shortcode in Widget
Have you ever wondered how to use Shortcode in Widget for WordPress? If you have noticed in general, even if you use Shortcode in a Widget area & would expect the proper output to be shown, that won’t be the case. So how to go about telling WordPress to parse shortcodes & execute the relevant function? Well, this is what this article is all about. In this article, I am going to share a very easy way to use Shortcode in Widget directly. Actual code is just 1 line, so it’s super easy to follow.
How to use Shortcode in Widget for WordPess
Before we move on to see how to use Shortcode in Widget for WordPress, let’s take a very quick look at what the terms “Shortcode” & “Widget” stands for, as far as WordPress is concerned.
It is simply a line of code that when used in conjunction with WordPress theme, widget, plugin, etc. executes the associated function, performs the related operations as written in the code & shows the output result. That’s it for the definition.
Simply put, WordPress Widgets enrich user experience by letting you add content and features to your WordPress theme sidebar(s). Some of the examples include, but not limited to default WordPress widgets such as the Blogroll, Search Box, Navigation etc. Some plugins often include their own custom widgets that can be added/removed from theme sidebar using WordPress Admin panel.
Ok, so how to use Shortcode in Widget?
It’s very simple. In order to use Shortcodes in sidebar widgets to be parsed as they are mean to be, simply edit the functions.php file of your theme and add the following code in it:
<?php add_filter('widget_text', 'do_shortcode'); ?>
Once you have entered the above code, save the file & reupload back to the theme folder. So by using the Appearance > Widgets menu option via Admin panel, you can now add the corresponding widget (such as the Arbitrary HTML widget) and enter the shortcodes you desire & save it. Once done, if the shortcode has the corresponding function properly, it should now execute the shortcode & show the relevant output.
Do you know of any other ways to use Shortcode in Widget for WordPress? Feel free to suggest by commenting below.