Need to use shortcodes in WordPress theme widgets? I propose here 2 solutions to do this. The first consists of 2 lines of code to add to the.php function file of your site's theme, the second is a plugin.
Enable shortcodes in widgets with the file functions.php
To enable shortcodes in WordPress widgets, you must 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 are added in this order. The function
shortcode_unautop() (codex) ensures that shortcodes are not surrounded by tags
<p>...</p>. As for the function
do_shortcode() (codex), it allows you to operate the shortcodes.
Enable shortcodes in widgets using a plugin
It is also possible to use a pluginShortcode Widget to make shortcodes usable in widgets.