WordPress has many somewhat hidden features and one of my favourites are shortcodes. In this post we’ll learn exactly what they are, when to use them, why they’re so brilliant and how to use them.
Shortcodes. What are they?
In a nutshell, shortcodes are shortcuts of writing phrases or words. Here’s an example. On WPShout whenever I write WPShout it always links to the homepage. That’s because I’ve set up a shortcode so all I have to do is type WPShout and WordPress outputs <a href="http://wpshout.com">WPShout</a>.
Another example – if I type WordPress themes WordPress outputs <a href="http://wpshout.com/category/themes/">WordPress Themes</a>. This allows me to quickly cross promote other sections of the site and link to other pages with minimal effort on my part. I do use them quite a lot – why not [f] or subscribe by RSS? All I’m typing is [f] and subscribe by RSS.
So how do I use them?
Now that you’re convinced shortcodes are the best thing since sliced bread, you’ll obviously want to know how to use these shortcodes. It’s really easy. First, you’ll need the functions.php file open. We’ll come back to that in a sec – first lets look at the WPShout example I showed earlier. In my functions.php file I’ve got the following:
function wpshout() {
return '<a href="http://wpshout.com">WPShout</a>';
}
add_shortcode('wp', 'wpshout');
Which as we’ve seen outputs a link to WPShout.com. The important thing to learn from the example is the formatting: start off with a function, then return what we want to be displayed and then add the shortcode. The first needs to be what you want to shorten to and the second the same as the function. Another example – I want WordPress to output ‘cheese’ when I type Creating an Advanced Theme Options Page in WordPress. Using the same formatting as the last example, you’d have:
function cheese() {
return 'cheese';
}
add_shortcode('c', 'cheese');
So when should I use them?
All the time actually. They save an awful lot of time for useful little things. Enjoy!
You should follow me on Twitter
Related Posts
No related posts.









2 Comments - Add Yours!
Thanks for the awesome post – I’ve been looking for a plugin that does this and had no clue it was easy to do on my own!
I’m having trouble implementing it, though. Just for testing, I copied your code and pasted it into my functions.php file (actually custom_functions.php – I’m using Thesis)
function wpshout() {
return ‘WPShout‘;
}
add_shortcode(‘wp’, ‘wpshout’);
So to insert this shortcode, I’d need to type [wp]?? Or [wpshout]? I tried doing both in a blog post and it just shows as plain text, the link isn’t showing up. Is there something I need to change to make shortcodes work?
.-= Carrie´s last blog ..Whole Foods deals: through November 3 =-.
OK never mind, I figured it out – which is what usually happens when I ask question.
.-= Carrie´s last blog ..Whole Foods deals: through November 3 =-.
3 Trackbacks