Time Saving WordPress Shortcode Tricks

One of my favorite features of WordPress is the shortcode trick.  The name says it all, but for those not familiar yet with the magic of shortcodes, they allow you to insert a particular function automatically into a WordPress page or post.  This function can be virtually anything.  From inserting recent posts to displaying a widget to affecting formatting, shortcodes can do it all.  Most premium themes are coming bundled with some shortcodes and it is quickly becoming the hallmark of a quality theme, at least those that are sold in marketplaces like ThemeForest.

From a functional standpoint, shortcodes are located in the theme files or plugin files.  When a particular shortcode is used in the post or page content, it references a longer snippet of code that creates the desired function.  So rather than trying to insert raw PHP into post content, I could write [myshortcode] and it would insert whatever longer snippet is referenced by that shortcode.  Nifty huh?

shortcodesIf you don’t have shortcodes but would like to play around with them, consider the Elegant Shortcodes plugin from Elegant Themes.  It is a standard WordPress plugin that adds the functionality of shortcodes that come with all of Elegant Themes premium themes.  They include buttons, info boxes, toggle boxes, tabs, a slideshow, an image slider, password protected content, columns, social media buttons, tooltips, tables, dropcaps, quotes and more.  Really anything you could need to spice up your site.

But enough about that one plugin.  Because I’ve found shortcodes so useful since they were introduced in version 2.5 that I thought I would push the limits of what they can do and look for some tips, tricks and hacks that extend their usefulness even further.  Disclaimer: I haven’t tried all of these in a live WordPress installation.  As always, backup your site before messing with it.

 1. Add shortcodes to theme files

Shortcodes are so easy to insert in posts and pages.  But what if you want to modify your theme and add a shortcode to it?  I’ve wanted to do this when I’ve wanted shortcode results to be displayed in a sidebar or somewhere else outside of the content area.  Adding a shortcode to the theme is simple.  Just insert this code, obviously replacing [shortcode] with your shortcode name.

<?php echo do_shortcode("[shortcode]"); ?>

Want to use a shortcode within the loop?  Try this:

echo do_shortcode('[shortcode]');

2. The “bloginfo” shortcode

The bloginfo function can display all sorts of handy information about your site which could save you time.  In order to use this function, you can built a very quick shortcode by inserting the following code into your functions.php file.  Thanks to CSS-Tricks for this one.

function digwp_bloginfo_shortcode( $atts ) {
   extract(shortcode_atts(array(
       'key' => '',
   ), $atts));
   return get_bloginfo($key);
}
add_shortcode('bloginfo', 'digwp_bloginfo_shortcode');

Then you use a shortcode with a parameter to tell the function which data you are looking to display.  Like this:

[bloginfo key='name']

You can also use a shortcode in HTML which can prevent you from having to write long URL’s over and over.  Like this:

<img src="[bloginfo key='template_url']/images/logo.png" alt="[bloginfo key='name'] logo" />

See the full list of info you can grab here, but you can choose from some of these beauties: name, description, wpurl, admin_emai and rss_url.

Note: make sure to switch to the HTML editor when adding a shortcode.  The WordPress WYSIWYG will strip the brackets from your shortcode if you enter them in the visual editor.

3. Allow shortcodes to be used in widget text

If you’re not into coding shortcodes directly into your theme files, but want them in your widget text, you can use this code to add a filter to the widget_text() function which overrides WordPress’ tendency to disallow shortcodes in widget text.

add_filter('widget_text', 'do_shortcode');

What is your favorite technique, trick or tip when working with shortcodes?  How have you used them to make your life easier?  Let us know in the comments below.

I created pingable back in 2007 and I still love blogging about Wordpress.