Wordpress Utilities Plugin

My latest project was my most complex Wordpress development yet. Rather than start with an existing template and customise it to meet my needs I actually started a new template from scratch. I wanted to do this so I could learn about more about the way Wordpress worked at it meant that the included files and code would only be exactly as I needed, nothing more and nothing less.

Once I had finished the first page (index.php) it became clear that things would get repetitive pretty quickly. The index page contained quite a lot of extra PHP code which was cluttering it up and would need to be reused on other pages. After managing to create a pepper for Mint I felt pretty confident that I could create a personal plugin that could contain any code I needed to use more than once and which could eventually be transferred to other projects.

Create your own Utilities Plugin

Creating a plugin for Wordpress is really simple. First of all create a new file in your /wp-content/plugins/ folder. Call it what you like, though I am using ajp_utilities.php I added my initials to the filename as it seemed to make sense. This is a personal plugin after all.

Next up, add the following to your new php file.

<?php
/*
Plugin Name: Personal Utilities
Plugin URI: http://www.criticalwebdesign.co.uk
Description: Contains useful functions
Author: Andy Pearson
Version: 1.0
Author URI: http://www.criticalwebdesign.co.uk
*/
?>

Obviously you will need to replace the values I have added with something a little more related to you! That’s all you need to do to create a Wordpress plugin. You can now upload this to your plugin directory and activate it using the Wordpress admin. At this point though, the plugin is not going to be very useful we need to add some functions.

As an example let’s create a function based on Christian Montoya’s Hot Dates with CSS which I have used a couple of times in the past. The original code is included in the Wordpress Loop. That works fine, but what if I wanted to use the code on the archive page, the home page, and the search results page. That is going to be quite a bit of repetition. Also, what if I wanted to change the output code, I would have to change it in three different places.

It’s not going to take too much effort to wrap up this snippet into a function and it will save time in the long run. The finished function would look a little something like the one shown below.

function ajp_hotDate($time)
{
    list($mo, $da) = explode(" ", $time);

    $o  = '<acronym class="published" title="';
    $o .= get_the_time('Y-m-dTG:i:sO').'">'."n";
    $o .= '<span class="pub-month">'.$mo.'</span>'."n";
    $o .= '<span class="pub-date">'.$da.'</span>'."n";
    $o .= '</acronym>'."n";

    echo $o;
}

This function could be called anywhere in The Loop by using the following code.

<?php ajp_hotDate(get_the_time('M d')) ?>

Also notice that for this function I have used my initials to prefix the actual function name. This will mean there is much less chance of colliding with other functions and is a good habit to get into when you are working with other people’s code (such as the large array of Wordpress plugins) or code you may release to the public.

What Next?

As you can see, it’s pretty easy to create a plugin for Wordpress and use it to keep your code concise and organised. Breaking out repeated code into it’s own function can save you time and effort so it is really worth doing.

Of course, this post only scratches the surface of what a Wordpress plugin can actually do. If this has whet your appetite why not read about plugins on the Wordpress codex. Make sure you check out filters, which are hooks that allow you to apply your own code around existing Wordpress functions.


Post Meta

Comments

Comments make the world go round, where would we be with out good discussion? Although free speech is encouraged, please make sure you keep on topic. Any abusive posts or spam will be dealt with accordingly. Speak to people how you would like to be spoken to!

  1. Montoya

    Gravatar

    March 25th, 2007 at 3:22 pm

    That’s a good intro to making a plugin (and thanks for using my code as an example!), but you can also use a functions.php file in the theme and include that from the header.php, if you want to write functions that will be specific to the theme you are working with. I know quite a few themes use that route, especially those that are for public distribution.

  2. Andy Pearson

    Gravatar

    March 25th, 2007 at 5:33 pm

    @Montoya - That’s a great tip about using a functions.php file, I hadn’t really considered that but it also makes total sense! I guess it’s just the thing when Wordpress and php are so flexible, there are 100 different ways to solve the same problem!

Trackbacks

Trackbacks let you see who have linked to this post, and what they have said about it.

  1. Plugin building - it's easy : Wordpress guy

    March 25th, 2007 at 12:48 pm

Leave a Reply

(required)

(will not be published) (required)