Add Shortcodes to Text Widgets

WordPress ShortcodesAre you thinking of using a plugin that uses shortcodes? (Shortcodes are tags in square brackets like [bubbles id="10"] that some plugins use to embed content into posts and pages.)  But what if you want to be able to use a shortcode in your blog’s sidebar instead of your post or page?

Normally, shortcodes are ignored if you try putting them in a sidebar Text widget — all that’s displayed is the shortcode itself.  But there is a one-line tweak you can add to your theme that will enable you to put any shortcode you like in a sidebar Text widget. Here are some steps to get you started:

  1. Login to your blog’s administration pages and go to the Theme Editor — i.e. select Appearance > Editor from the admin menu.
  2. Look in the list of Template Files on the right of the page for one called Theme Functions or functions.php and click on it to load it into the editor.
  3. Find a place between the <?php and ?> tags which is not in the middle one of the functions that may already be in the file (the very top or bottom of the file are both good places) and add the following line:
    add_filter('widget_text', 'do_shortcode');
  4. Click on the Update File button to save the modified file.

Assuming you didn’t mess anything up, go to your Appearance > Widgets page. Drag a Text widget to the sidebar area. Add your shortcode into the widget and save. The shortcode will now be displayed in the sidebar of your blog.

Well done.

About Clifton Hatfield

Clifton is the kind of guy that can read code better than he can read a book and prefers it that way. He likes cold pizza, moving sidewalks, and Indiana Jones. Clifton co-founded Internet Lifestyle Network from his home office in 2012 and he believes that true leadership is to inspire.
1 comments
Claire Harbour
Claire Harbour

Hello Clifton, Great tut! I've been looking to do this for some time, but haven't been able to work it out myself. I love shortcodes. I just wanted to say that I love the new design on your blog! It rocks! :) ~ Claire