DiggingIntoWordPress

by Chris Coyier & Jeff Starr

Speed Up Your Blogging with WordPress Shortcodes

Posted by on

Save time by replacing your most commonly typed words and phrases with WordPress shortcodes. For example, if you are frequently typing your blog’s URL, you could place the following code your theme’s functions.php file:

function shortURL() {
	return 'http://your-site.com/';
}
add_shortcode('myurl', 'shortURL');

Now whenever you write a post in “HTML-mode”, you can include your blog’s URL at breakneck speed by simply typing “[myurl]”. Works great in WordPress 2.5 or better.

Shortcodes for your favorite links
To create a shortcode for your favorite links, we need to include the href attribute information as well as the anchor text for the link itself. We can do this by placing this function in your theme’s functions.php file:

function shortLink($atts, $content = null) {
	extract(shortcode_atts(array(
		"href" => 'http://default-website.com' // default URL
	), $atts));
	return '<a href="'.$href.'">'.$content.'</a>';
}
add_shortcode('link', 'shortLink');

Then, when creating a post, emulate the following format to include any links you wish:

[link href="http://your-site.com/"]Your Website![/link]

..which will output the following code:

<a href="http://your-site.com/">Your Website!</a>

When the href attribute is removed from the shortcode, the default URL will be used. You may specify the default URL in the third line of the function (see comment). This makes it easy to add a complete link to your site on the fly by simply typing:

[link]Your Website![/link]

One of the nice things about using shortcodes instead of hard-coded links is that you can easily change the default URL without breaking anything.

6 Responses

  1. Yep, shortcodes come handy lots of times. I use them for getting a list of posts into articles.
    The following code get the last 5 posts with category 16 by default by using [list] but one can set the number and cat like so [list num=”3″ cat=”45″]

    function sc_liste($atts, $content = null) {
         extract(shortcode_atts(array(
              "num" => '5',
              "cat" => '16'
         ), $atts));
         global $post;
         $myposts = get_posts('numberposts='.$num.'&order=DESC&orderby=post_date&category='.$cat);
         $retour='<ul id="news">';
         foreach($myposts as $post) :
              setup_postdata($post);
              $retour.='<li><a href="'.get_permalink().'">'.the_title("","",false).' &raquo;</a></li>';
         endforeach;
         $retour.='</ul> ';
         return $retour;
    }
    add_shortcode('list', 'sc_liste');

  2. thnx Jeff, n Andreas too.. i will be practicizing those snippets..

  3. djavupixel September 28, 2010

    Nice article thanks.
    I also like the plugin Widget on Pages (http://gingerbreaddesign.co.uk/wordpress/widgets-on-pages/) as it can achieve this stuff too. Take a look it is well worth it.

    • Hey djavupixel,

      I’m the dev for that plugin, thanks for the plug and pleased you like it.
      And as Jeff and others have stated shortcodes are immensely powerful and can add a huge range of formatting power to the end users of WordPress.

  4. graphicbeacon October 16, 2010

    There are way more uses for wordpress shortcodes. Pippin Williamson has dealt with this extensively and I’ve also taken it a step further:

    http://www.problogdesign.com/wordpress/working-with-wordpress-shortcodes/

    http://www.graphicbeacon.com/how-to-add-a-paypal-donation-shortcode-button-to-your-posts/

    http://www.graphicbeacon.com/execute-multiple-custom-post-type-queries-using-wordpress-shortcodes/

Comments are closed. Contact us with any critical information. Thank you!

Code is poetry