A Quick (Yet Functional) Intro to Shortcodes in WordPress

Let’s start off with a bit of history, shall we? Shortcodes were first introduced to WordPress in version 2.5 (way back in March of 2008) so they’ve been around for a while. Still, after seven years in WordPress, there’s a bit of confusion about what they are and what they can do.

This functional little primer will help you understand not just what they are, but how and why you should play around with them (even if you’re not a serious developer!).

Why Use Shortcodes?
Simply put, WordPress shortcodes are “macros” that enable you to put code inside your blog posts and, in doing so, maximize your content. These small snippets of text can have a huge impact on your productivity.

Here’s an example: Let’s say that you consistently write the same type of content every day, which includes design elements and a bit of code to display some other pieces of content (like other posts that you’ve written). Instead of copying and pasting this content every single time you sit down to publish, now you can use one word in your content body and voila! It’s done.

That’s what a shortcode can do for you. Now, let’s go ahead and build our first one.

Creating Your First Shortcode Called “Whoot”
Now you’ll have to get your hands a little bit dirty in code. I promise you won’t do anything that you will regret, but make sure to back up your files before you attempt anything (generally speaking, not just for making your first shortcode!).

  1. First, create the shortcode function in your functions.php file.
  2. Register a unique name.
  3. You’ll call that unique name in your content.

That’s it! Let’s get started, shall we?

First, add this super-duper-long (kidding!) piece of code to your functions.php file:

// Add Shortcode

function WhootWhoot() { return ‘Wow, not bad! I built my first shortcode in WordPress!’; }

add_shortcode(‘whoot’, ‘WhootWhoot’);

Put it at the very end of your functions.php file, like this:

widgets1

You don’t have to understand the specifics, but the point of using this example is to show you how much work a shortcode can do for you when you create them properly!

Take a look at the very last line:

add_shortcode(‘whoot’, ‘WhootWhoot’);

You’ve created a new shortcode that you’ll call “whoot” in your content body whenever you need it. Let’s go ahead and do that, shall we? Create a new blog post and insert this exactly:

[whoot]

It should look like this:

widgets2

Nothing to it, right? What do you think should show up on your blog post? That’s right! It’ll show the content that you wrote in the function: “Wow, not bad! I built my first shortcode in WordPress!” See it here:

widgets3

Not bad, right? You’ve just created your first shortcode. I hope you’ve gotten a decent idea of how crazy powerful widgets can be when used correctly! Imagine all the possibilities of being able to create tons of content, all the time, with just one-word snippets! In fact, with the help of this WordPress Shortcode Generator, you should be able to take your newfound skills to the next level!

Don’t forget to also check out the following resources:

Have fun!

About the Author More by this Author