WordPress Quick Tips #3:
Adding a Shortcode to a Sidebar Widget

tweaker

Are you thinking of using a plugin that uses shortcodes? (Shortcodes are tags in square brackets like [az-index id="1"] that some plugins use to embed content into posts and pages.)  Do you want to be able to use a shortcode in your blog’s sidebar instead of your post or page?

Well, if the answer to both those questions is “Yes” then you have come to the right place!

Normally, shortcodes are ignored if you try putting them in a sidebar 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 widget.  For example, one AZIndex user recently wanted to display an index generated by the plugin in his blog’s sidebar.  These are the steps he followed:

  1. Login to your blog’s administration pages and go to the Theme Editor — i.e. select Design >> Theme 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 template.

That’s all you need to do.  Now any shortcodes you add to a sidebar widget will be correctly processed by the plugin they belong to.  The easiest way to test this is as follows:

  1. Go to Design >> Widgets using the administration menu.
  2. Find the Text widget in the list on the left, and click its Add button.
  3. Click on the new widget’s Edit link, and enter a title for the widget and then add the shortcode below it.
  4. Click the Change button, then click the Save Changes button (easy to forget) and then go to the front page of your blog to see your new shortcode-enabled widget.

(Note: your theme must support widgets for the new text widget to show up.  If it doesn’t then you will have to “widgetize” your theme or find another theme that does support widgets.)

So if you have been itching to use AZIndex to put an index of posts in your sidebar (just not too big, I hope) then now you know how to do it!  Of course, this tip will work for any plugin that uses shortcodes.

  1. Bullion said,

    Great tip on adding shortcodes to the text widget. To use your shortcodes outside of posts, pages, and the text widget, you could always call your shortcode function directly like this:

    $text = yourShortCodeFunction(’[your shortcode tag here]‘);
    echo $text;

    The above code will allow you to use wordpress shortcodes anywhere in your blog. Keep up the good work.

    I really like how you can preview your comment before submitting, is that a wordpress plugin?

  2. Andres said,

    Hi.
    I’ve installed your plugin and am using it at http://no.ticiero.com/noccionario/

    I’d like to add the az index at the bottom of all posts within a particular category. Is there any way to add this into the php for that template rather than add it manually to each post in that category?

    thanks for hte great plugin.

    Andres

  3. JenniferB said,

    Hi,

    I have tried this tip in order to use it in combination with a new widget called Widget Changer but it is not working as you can see in the sidebar of this post:
    http://jenniferbrinckmann.com/journal/homemade-italian-vinegar/
    Can you point me in the right direction, please?

    Any help would be greatly appreciated.

    Best regards,

    Jennifer

  4. Mona said,

    Its a very interesting and cool post about online shopping. people are using online shopping to buy any product.
    Thanks for your time to write this post.

  5. Kisu @ ClassroomTeacher said,

    Hi English Mike,

    Tried the tip but couldn’t get it to work on our site. (Trying to use it with posting a single pic with the NextGEN gallery short code. Something like, [singlepic=id,width,height,web20,center] where the variables are numbers. The code works fine in a post, but doesn’t get filtered in the widget, even with your little hack.

    Still, it was a nice try. Thanks for the tip. I might try Bullion’s tip and simply use a PHP Exec plugin to run php in the widgets. Cheers, Kisu

  6. How to: Add shortcodes in sidebar Widgets said,

    [...] goes to English Mike for this awesome recipe! Did this tip help you? If so, feel free to make a small [...]

  7. neden para icat said,

    thanks for this info…

Leave a Comment

(See below for a live preview as you type)

(See below for a live preview as you type)