Enabling shortcodes in widgets, quick WordPress tip.
Let’s say you’re developing a plugin that registers a shortcode so that a user can output some kind of form in his posts. Now, the user wants to output the form in one of his widget area’s. One way to accomplish this would be to explicitly create a widget for it, add some fields like ‘text_before’ and ‘text_after’ and use that to output and add some before and after text to the form.
An easier approach would be to just enable the shortcode so that your user can type some text in one of WordPress’ default text widgets (or a WYSIWYG Widget, to make things a bit easier) and use the shortcode wherever he wants to output the form, or whatever it is your shortcode provides.
By default, shortcodes in text widgets are not enabled in WordPress. However, since the text from a text widget runs trough a filter called ‘widget_text’, enabling them would be pretty easy. In short,everything that’s needed is one simple line of code:
Just using this line of code could lead to some problems though. Best thing would be to add a priority so that do_shortcode runs after wpautop (the function that converts linebreaks into paragraph’s). Looking at wp-includes/shortcodes.php you’ll find that the default shortcode filter priorty is 11 so that is what we’re gonna pass as a 3rd parameter.
add_filter( 'widget_text', 'do_shortcode', 11);
Now, to ensure that the output of the shortcode function does not get wrapped in <p> tags because of wpautop it is best to add one more filter, ‘shortcode_unautop‘.
add_filter( 'widget_text', 'shortcode_unautop'); add_filter( 'widget_text', 'do_shortcode', 11);
Note for less tech savvy readers: the above snippet goes into your theme’s functions.php file.
Of course you can use the above snippet for pretty much every filter that’s out there, comment_text, term_description, manual filters, anything really.
If you’re a plugin author and you have a custom widget, another way to enable shortcodes inside your widget is to run the content trough the do_shortcode function. So, let’s say your Widget’s content is stored inside a variable called $text, you’ll pass this variable as a parameter to the do_shortcode function.
... $output_text = do_shortcode($text); echo $output_text; ...
Another, more flexible way would be to create a filter for your widget and hook the do_shortcode function to that filter. But, if enabling shortcodes is all that you’re looking for, the above snippet will do just fine!
Got a nice addition to this post? Let me know trough a comment!