Shortcodes in Sidebar Widgets

Edit: As of WordPress 3.3, shortcodes now work in widgets (the first line of code has been added to WP core). But you’ll still need to create the other functions.

WordPress doesn’t support shortcodes in sidebars by default, but it’s easy to fix. I’m going to focus on a specific use of shortcodes in a sidebar, but you could use this technique for any shortcode.

When migrating a WordPress website from my development server to a client’s, I do a find/replace in the database for the old URL’s. But the URL’s in the sidebar remain unchanged, due to the way they’re stored in the database.

WordPress gives you some handy functions for displaying the site’s url ( bloginfo('url') ), the wordpress directory ( bloginfo('wpurl') ), and the child theme directory ( bloginfo('stylesheet_directory') ). I now create three shortcodes and use them instead of hardcoding URL’s in the sidebar. This is useful if you’re displaying images in the sidebar and don’t want to manually update them after migration.

Bill Erickson

Bill Erickson is the co-founder and lead developer at CultivateWP, a WordPress agency focusing on high performance sites for web publishers.

About Me
Ready to upgrade your website?

I build custom WordPress websites that look great and are easy to manage.

Let's Talk

Reader Interactions

Comments are closed. Continue the conversation with me on Twitter: @billerickson


  1. Dorothy says

    Looking for instructions on the “Div Shortcode” widget you’ve provided in WP. Widget not showing up on admin – appearance – widget page.

    • Bill Erickson says

      Div shortcode isn’t a widget – it’s a shortcode. If you’re writing a post and you wanted to wrap some content in a div with a class of “alert”, you could do this (without the space between [ and div):

      Standard post text

      [ div class=”alert”]

      My alert text goes here

      [ end-div]

      Standard text continues

    • Bill Erickson says

      You can either add it to your theme’s functions.php, or a better solution would be in a core functionality plugin. That way this functionality isn’t tied to the current theme, and when you change themes you don’t lose the functionality.