Advertisement
  1. Code
  2. Plugins
Code

Multiple Shortcodes With a Single Function: 3 Killer Examples

by
Difficulty:BeginnerLength:QuickLanguages:

The powerful Shortcode API allows us to create "snippets" and include special content (such as PHP functions or complex HTML code) in our posts. It's extremely common to build separate functions for each shortcode, but is it really necessary? In this post, we're going to see how we can utilize more than one shortcode by building a single function.


In Defense of Shortcodes

The APIs make WordPress one of the most flexible content management systems of all. They are the reason why there are tens of thousands of plugins and themes out there: The huge community of WordPress is capable of making WordPress do backflips and cartwheels, thanks to the APIs of WordPress.

Among these APIs, my favorite is the Shortcode API. Its logic and validity is being questioned but I strongly believe that using shortcodes in WordPress has an easy learning curve and once they are learned those few easy rules about parameters and opening/closing tags, even novice users can enjoy using shortcodes. Correct, it's not WYSIWYG but a WordPress newbie may have a harder time to clear the mess if they did something wrong in a WYSIWYG editor and ruined the HTML. Easier to see that the shortcode doesn't work, delete it, and type it again.

But of course, it's just an opinion of mine. Let's get back to the subject: We're building multiple shortcodes with a single function!


The Benefit of This Technique

There's really no need to argue the benefit of this method - consider this ridiculously long example:

The same code (both PHP and HTML) is being used over and over again in every single function - except the varying CSS classes. Plus, the attributes in each function are the same as in the other functions. It's just hard to read. Come to think of it, if we ever had to change a feature of these column divs, we would have to change the same parts in all of the functions.

What if there was a variable to fetch the "tag" of the shortcode? Not so surprisingly, the variable is called $tag and could be used inside our functions. It allows our functions to check the tag of the shortcode and behave according to the tag.

Think about the example above: If there is more than one function that serves almost the same functionality, it would be logical (and pretty cool) to have one function for our shortcodes to use.

I've come up with three good examples on how we can use this technique. If you can think of more while reading the post, you're welcome to share your thoughts with everyone else on the comments section below!


Example 1 Video Embeds

In one of my earliest posts on Wptuts+, I explained how to use a shortcode to embed videos from various video hosts like YouTube, Vimeo and Dailymotion. The code looked like this:

While it's still a not-so-bad practice (except the 6 else if statements), we can now use one less attribute ('site') and create separate shortcodes, like this:

See what we did? We provided a $tag parameter for our function in the first line (to replace the "site" attribute) and used a switch conditional statement (for the sake of better, cleaner code). The result for the two functions will be the same, except you can use [youtube id="..."] instead of [vid site="youtube" id="..."] now.

(I didn't use some of the video hosts in the second function. If you need to add more hosts, you can add as many "case"s as you like.)


Example 2 Column divs

Ah, columns... they're the most important part of a CSS grid, and they make it super easy to make your website more adaptive, if they support responsive design techniques. You can see them in almost every WordPress theme on ThemeForest and they use shortcodes... with a practice like our "ridiculously long example" at the beginning of this tutorial.

As you remember, the code was almost the same in all 5 functions in that example. Thus, it would be incredibly easy to merge them into one single function and let the $tag variable work its magic:

Pretty neat, right? We didn't even need to use a switch!

We still duplicate lines while adding the shortcodes, though. Dare to go one step further? Remove the add_shortcode() lines to replace with this:

Now we have even more maintainable code. For example; if we were to change the name of the function, we wouldn't bother to change every line anymore.


Example 3 Post Lists

Ever needed to list some previous (or future) articles in your posts? I certainly do. There are loads of plugins which provide shortcodes but most of them require you to use tons of attributes which can result in some ugly, complex tag like [posts cat="5,6" author="barisunver" status="private" postsperpage="4" and="so on"].

How about we use our beloved $tag instead? Let's give it a shot:

As you can see, we can get rid of 3 possible shortcode attributes: "cat", "author" and "post_status". You can always extend the cases and add new shortcodes with this single, relatively small function.


Wrapping Up

As a fan of WordPress shortcodes, this discovery of the $tag variable is kind of exciting for me. It seems like there's a huge potential to utilize this method.

What do you think about this technique? Post your comments below and share your thoughts with us!

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