Just another Freebytes.eu weblog

Shortcodes in Widgets

I had occasion the other day to run a shortcode inside of a text widget. You know shortcodes… we talk about them all the time. They are keywords in [square brackets] that do something special. Sometimes something really simple like returning a string (so you can have a global location to change that string) or something complicated like call a plugin that does something fancy like build a photo gallery.

But alas…


This ain’t gonna work “out-of-the-box”

After tinkering with some far-too-complicated solutions, someone on Twitter helped me with a deliciously simple solution: Just add this to your functions.php file:

add_filter('widget_text', 'do_shortcode');

This will ensure that the text content of widgets is parsed for shortcodes and those shortcodes are ran. Awesome.

Stephanie Leary wrote a great article on Using Shortcodes Everywhere, which covers this as well as using shortcodes in a bunch of other places: Comments, templates, excerpts, etc.

Like the article? Get the book!


© 2010 Digging into WordPress | Permalink | 4 comments | Add to
Delicious
Categorized: PHP | Tagged: ,