1. Code
  2. General

Using Shortcodes to Speed Up Posting with Popular Services

Read Time:9 minsLanguages:

Today, we're going to look at how to use shortcodes in conjunction with a variety of popular services - including sites like YouTube and Flickr by first recapping how to create a shortcode, and then how to convert that third-party code into something that can be repeated and manipulated.

We've covered shortcodes already here at WPTuts+, discussing the advantages of using them and how to actually, well, use them. Shortcodes are effectively shortcuts (as defined by WordPress themselves) that allow an outcome to be achieved repeatedly with the least work possible. In their simplest form, a shortcode includes some code you've hooked into WordPress in another file, sometimes the product of a third-party, such as a YouTube video or a Flickr stream.

Recap: Creating a Basic Shortcode

If you're not familiar to shortcodes, then you should probably stop reading this article and check out one of the other articles we have here at Tuts+ about shortcodes in WordPress.

A shortcode is created by adding a function to your functions.php file, and then linking it up as a shortcode. The example below creates a link to the AppStorm hub.

To use it, we use the simplest form of a shortcode with no attributes.

We can take this idea further by introducing an attribute, in which to specify a specific AppStorm site to link to.

The time, the shortcode is linking to a subdomain of AppStorm. We specify which subdomain by adding the site attribute. If there is no attribute defined in the shortcode, the shortcode defaults it to www which just links back to the main homepage. The usage example below would link to iPad.AppStorm.

As you can see, shortcodes are pretty self-explanatory. What we want to do, however, is use the same basic principle of a link generated by a shortcode with an attribute to use in conjunction with a third-party service like YouTube or Flickr.

From Embed to Shortcode

Converting a static third-party piece of code to a shortcode is pretty similar to the method used to create a link. Simply, we're going to create a function that returns the code as a shortcode and then throw in a few attributes to customise it.

Step 1. Creating A Non-Variable Shortcode

The first step we should take is to take the regular embed code, and create a simple return function out of it.


In the same way we created that first, non-variable link in the first example, the piece of code above embeds a YouTube video that will not change at all. And how it does that is pretty self-explanatory too.

Step 2. Introducing Some Attributes

Essentially, the piece of code in the previous step will produce a result identical to if the returned code was pasted into the editor itself. Unless you are going to be regularly embedding the same video over-and-over again, this shortcode is probably useless. Therefore, we want to introduce some attributes to translate into parameters for the iframe.

In order to achieve this, we once again use the extract() function to pull attributes in from the shortcode. In the code below, we do this in a basic form by only creating an attribute to replace the video ID.


And, just like substituting the video ID, we can do the same for the width and height. This is the time you'll want to make sure you're providing default values for, however, in case the user doesn't provide a width or height value.


Step 2b. Additional Parameters

YouTube does have additional parameters that can be added to the URL, such as autoplay. Obviously, these can be applied to the returned code too, with their own attribute if necessary. Generally, this is self-explanatory and is demonstrated below (added parts are in bold).


Step 3. Done!

It really is that simple, and, as you'll see in the following roundup, the same basic method can be used to create shortcodes for other third-party services.

Additional Examples

All these third-party embeds have been created by using the same steps as above. Of course, like in step 2b, additional parameters can be added at will.


A Vimeo video is embedded in pretty much the exact same way as a YouTube video, through an iframe with width and height variables. The only addition is the color variable to customize the player.


Twitter (Profile)

The Twitter profile widget is a piece of JavaScript, with a ton of different attributes, all substituted for shortcode attributes in the code below. Because there is so many, you may want to remove some and replace them with constant values (such as styling) in the actual JavaScript, or just change the default attributes in the array and forget about using them in the shortcode.


All of these shortcode attributes have defaults, except the username which is required. As to what to actually put in the attribute, they are all self-explanatory up to hashtags in which the rest are just true/false.

Tweet (Button)

The Tweet Button is a popular social sharing button for Twitter.


The tweet button's first two attributes are not really ambiguous. The last one sets the style for the tweet button, either horizontal, vertical or none.

Facebook Like and Send Buttons

Like the Tweet Button, the Like and Send buttons are the social sharing buttons for Facebook. Please note these buttons also need the JavaScript SDK referenced somewhere on the current page.


The different attributes for use are explained on Facebook's associated page, but essentially, the width is written in the same format as before, showfaces is a true/false value of whether to show faces of the "like-ees", colorscheme is either dark or light and font is, well, a font.

Since the XFBML will automatically target the current page, it's not necessary to have any attributes in use, meaning the shortcode can be as simple as the below example.

Flickr Badge

The Flickr badge is a way of displaying your recent photos from Flickr. Unfortunately, the Flickr widget comes with it's own CSS (although you can easily move this into your stylesheet), and uses tables for layout. Once your finished being highly frustrated over this practice, here's the shortcode.


Flickr's widget has four basic variables, the userid is the user's id (in the format of 12345678@N01), the num is the number of photos shown, the sort is either latest or random and relates to how the photos shown are determined and the size is either s (a small square), t (a thumbnail) or m (medium).


Hopefully i've explained how to create a shortcode in the context of a third-party service. It is fairly simple, we just need to work out how the original code works and swap out static variables for attributes. This saves time, but, perhaps more importantly, means that the code can be modified at a later date whilst maintaining the parameters allowing you to change, say, the width at a later date across all uses of the particular shortcode.

Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.