WordPress 🎁 Enable shortcodes in widgets

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

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.

Julien MA Jacob
WordPress Developer
Julien MA JACOB - WordPress Developer
Passionate about the web and programming with always many projects in mind
  Discussion of the article
Subscribe to the newsletter
Notify to
guest
1 Commentary
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Patric CODRON

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