In Part 1, we looked at planning out our plugin.
Part 2 was all about making the tables that would support it.
In Part 3 we began work on our admin panel.
Part 4 examined the benefits of using Object Orientated PHP which brings us to: Part 5
The final part, where we look at using WordPress’ nifty ShortCode functions.
The Magic of ShortCodes
The first thing that we do is open up our main plugin file, which in this case is tabberlist-main.php. Then we need to create a function (using our !function_exists check first), which I’m going to call tabberlist_display() because, well, it’s all about displaying the TabberList.
if (!function_exists('tabberlist_display')) {
function tabberlist_display($atts) {
}
}
Now that I have the function name defined, I go ahead and add the action script that will trigger the function when the page is loaded.
if (!function_exists('tabberlist_display')) {
function tabberlist_display($atts) {
}
}
add_shortcode('tabber_list', 'tabberlist_display');
The add_shortcode() function is a built in WordPress function that will be triggered on every page and post load. The first argument defines the name of the shortcode (’tabber_list’) and the second is a the function that is triggered if the name is found (the function we just declared, ‘tabberlist_display’). Now, the user can add a tabberlist shortcode to any post or page – which looks like [tabber_list], in case you’ve never used or seen one – and then the ‘tabberlist_display() function will then run.
Yup, that easy. We’re done here now, so thanks for reading!
Making it Do Stuff
OK, so it doesn’t do, well, anything. At least not obviously. If you copy this as it stands into your plugin file, then add a [tabber_list] shortcode into a post or page and publish it, then the [tabber_list] shortcode should not appear on the post. This indicates that everything is working correctly.
Wordpress is finding the shortcode, removing it from the post and running the function. But you want to be extra sure that it works, right? So we add this to our function to make sure that it really is working:
if (!function_exists('tabberlist_display')) {
function tabberlist_display($atts) {
$return = "Hello, world";
return $return;
}
}
add_shortcode('tabber_list', 'tabberlist_display');
Note that line 4 is very important – if you don’t return anything, WordPress won’t display anything.
So now if you add a [tabber_list] shortcode to any post or page, it should be replaced with ‘Hello, world’. Awesome.
ShortCode Attributes
But it gets better. Every instance of a ShortCode can be given unique attributes so that it functions differently in different situations. In TabberList, for example, I wanted the user to be able to select which categories would be displayed at any given time.
Using the logic that, if they don’t specify any categories, show them all and then, otherwise, show the specified categories, we can create an attribute for our TabberList which I have imaginatively titled ‘cat’.
Going back to our code, you’ll notice that the function tabberlist_display($atts) has an argument already ($atts). These contain any attributes that are declared within the shortcode and are separated by a space (e.g. [tabber_list cat=1,2,5]).
To get hold of our attributes, we can use a default PHP function and a built in Wordpress function, like so:
if (!function_exists('tabberlist_display')) {
function tabberlist_display($atts) {
extract(shortcode_atts(array( 'cat' => ''), $atts));
return $return;
}
}
add_shortcode('tabber_list', 'tabberlist_display');
The shortcode_atts() function takes the arguments given in ‘$atts’ and, in this case, puts them into a named array (’cat’). We could put in a default value (e.g. ‘cat’ => ‘1′) but, as we want the function to display all of the categories if the user hasn’t specified, then we leave the default blank.
We then use the PHP function extract() to get hold of the values from the array that was created by the shortcode_atts() function. Now that we have the values, we can do the following:
if (!function_exists('tabberlist_display')) {
function tabberlist_display($atts) {
extract(shortcode_atts(array( 'cat' => ''), $atts));
if ($cat == "") {
// Display all of the categories
} else {
// Display the selected category
}
return $return;
}
}
add_shortcode('tabber_list', 'tabberlist_display');
And there you have it. Thanks to WordPress’ awesome API, creating user friendly plugins is a piece of cake! What I’ve explored over the past few weeks is really only the very beginning of what’s possible with this incredibly extensible and easy-to-use platform and I hope that I’ve inspired you to explore plugin development much more deeply.
If you’ve enjoyed this introduction on creating WordPress Plugins, please consider subscribing to my RSS feed or to my email list.
If you have any thoughts, please Leave a comment and let me know! And don't forget to share!

In
Part 2 of my originally-titled Wordpress Plugin Development series continues. Today: plugin setup and database structures.
I’ve been asked by Adam Baker of 