WordPress 🎁 Enable shortcodes in widgets

Share on twitter
Share on linkedin
Share on facebook
Share on whatsapp
Share on email
Share on twitter
Share on linkedin
Share on facebook
Share on whatsapp
Share on email

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.

Julien .MA Jacob
Developer WordPress
Julien MA Jacob - Developer WordPress
🧙♂️ Passionate about the web and programming with always lots of projects in mind 👨💻 | About page
  Discussion of the article
Subscribe to our newsletter
Notify of
guest
1 Commentary
most popular
latest oldest
Inline Feedbacks
View all comments
Patric CODRON

Hello,
Small addition: since WordPress 4.9, shortcodes are automatically interpreted in the text widget (at last!).