admin管理员组

文章数量:1122846

I created a plugin and tried to use its shortcode in a Custom HTML Widget, but for some reason it's outputting [my_shortcode_name] instead of executing the shortcode.

The shortcode works fine if I place it on the page or in a Text Widget.

How can I make it work in a Custom HTML Widget?

I created a plugin and tried to use its shortcode in a Custom HTML Widget, but for some reason it's outputting [my_shortcode_name] instead of executing the shortcode.

The shortcode works fine if I place it on the page or in a Text Widget.

How can I make it work in a Custom HTML Widget?

Share Improve this question asked Apr 28, 2019 at 8:39 Pikamander2Pikamander2 6086 silver badges20 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 11

I was able to make it work by adding these two lines to my theme's functions.php file:

add_filter( 'widget_text', 'shortcode_unautop');
add_filter( 'widget_text', 'do_shortcode');

The Custom HTML Widget now correctly executes the shortcode function and outputs the result.

For the Custom HTML Widget it's better to use this hook:

add_filter( 'widget_custom_html_content', 'do_shortcode' );

本文标签: pluginsHow can I make my custom shortcode work in a Custom HTML Widget