Need to use shortcodes in the widgets of a WordPress theme? Here are 2 solutions to do that. The first one consists of 2 lines of code to add to the function.php file of the theme of your site, the second one is a plugin.
Enable shortcodes in widgets with the functions.php file
To enable shortcodes in WordPress widgets, you need to add these lines of code in the file
functions.php of the activated theme.
// Enable shortcodes in widget text add_filter ('widget_text', 'shortcode_unautop'); add_filter ('widget_text', 'do_shortcode');
It is important that these lines be added in this order. The function
shortcode_unautop() (codex) ensures that the shortcodes are not surrounded by
<p>...</p>. As for the
do_shortcode() (codex), it allows you to run the shortcodes.
Enable shortcodes in widgets using a plugin
It is also possible to use a plugin as a Shortcode Widget to make the shortcodes in the widgets usable.