Write Faster With WordPress’ Shortcodes

Write Faster With WordPress’ Shortcodes

Posted on 28. Oct, 2009 by Alex Denning in Quick Tips

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!

No related posts.

Tags:

Follow on Twitter! Subscribe!
Alex's Gravatar

Alex Denning is the founder of WPShout. A WordPress developer from London, Alex co-founded WPShift at the start of 2010 where he sells awesome WordPress themes.

You can find Alex on Twitter and at AlexDenning.com.

5 Responses to “Write Faster With WordPress’ Shortcodes”

  1. Carrie

    29. Oct, 2009

    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 =-.

    Reply to this comment

Trackbacks/Pingbacks

  1. [...] always posts awesome tips, tricks, and hacks for WordPress.  you can take a look at his post Write Faster with WordPress Shortcodes for another explanation of how they [...]

  2. [...] always posts awesome tips, tricks, and hacks for WordPress.  you can take a look at his post Write Faster with WordPress Shortcodes for another explanation of how they [...]

  3. [...] al ingresar en el post el shortcode marcialsoto.com. Fácil, ¿no?1 Este truco lo he aprendido de WPShout [...]

Leave a Reply

Please use your real name when commenting. Wrap code in <code> tags and make sure HTML is encoded. You can use regular HTML like <a href="... etc.

Get yours questions answered quicker

If you're using this post for paid work and have a question of any complexity then please ask at WPQuestions where you'll get a prompt response.