5

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?

2 Answers 2

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.

4
  • Mark your own answer as Accepted Answer to help other people searching solution for same issue. Commented Apr 28, 2019 at 9:40
  • 1
    @QaisarFeroz - You have to wait two days before accepting your own answer. It's a global Stack Exchange restriction. meta.stackexchange.com/q/6044/258412 Commented Apr 28, 2019 at 9:55
  • OK, Stay blessed. Commented Apr 28, 2019 at 9:57
  • Thanks, for me it worked only with the last filter: add_filter( 'widget_text', 'do_shortcode'); Commented Sep 4, 2023 at 9:11
1

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

add_filter( 'widget_custom_html_content', 'do_shortcode' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.