Like the blog? Get the book »

Speed Up Your Blogging with WordPress Shortcodes

Speed Up Your Blogging with WordPress Shortcodes

Shortcodes are WordPress’ way of creating shortcuts for code snippets, strings of text, or anything else that you need to display on your site. So this means that you can 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:

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. Andreas Ostheimer

    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().'" rel="nofollow">'.the_title("","",false).' »</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. Nice article thanks. I also like the plugin Widget 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. There are way more uses for wordpress shortcodes. Pippin Williamson has dealt with this extensively and I’ve also taken it a step further. Thanks!

Comments are closed for this post. Contact us with any critical information.
© 2009–2024 Digging Into WordPress Powered by WordPress Monzilla Media shapeSpace