Write Faster With WordPress’ Shortcodes

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 [wp] whenever I write [wp] it always links to the homepage. That’s because I’ve set up a shortcode so all I have to do is type [wp] and WordPress outputs <a href="https://wpshout.com">WPShout</a>.

Another example – if I type [themes] WordPress outputs <a href="https://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 [s]? All I’m typing is [f] and [s ].

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 [wp] example I showed earlier. In my functions.php file I’ve got the following:

function wpshout() {
    return '<a href="https://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 [c]. 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!