Need a WordPress website this weekend? Start here...

How To Build Your WordPress Plugin Development Infrastructure

(Reading time: 8 – 13 minutes)

So you wanna write a plugin?

Cool.

Writing a plugin is like accomplishing any other task:


The key to any endeavor is proper prior planning.

And understanding what you want to achieve.

If your goal is to adjust an existing plugin with some minor tweaks, you can pretty much stop right here. There’s plenty of information on the web that will get you going on an ad-hoc basis, and you can fiddle with it using Notepad and FTP.

Or try modifying a useful little plugin as explained in “How To Customize Estimated Reading Time Plugin for WordPress.” You can make these modifications directly in the WordPress plugin editor. If you mess it up, just delete the plugin and reinstall, no harm, no foul.

However… if you wanna run with the Big Dogs… you gotta get off the porch.

Are you sure you’re ready for this?

Sure you are!

What you need to know

It turns out the programming simple systems such as WordPress plugins is really not that difficult, provided you have the appropiate tools and development environment. In a nutshell, you’ll need to understand a little bit about:

  • Programming language syntax
  • WordPress system operation as a php/mysql web application
  • Network and server operations
  • Programming and development environments

That looks like a lot!

In one sense it is a lot. In another sense, it’s not so bad because you can break it down into independent steps.

From personal experience, and watching many, many other people, your first line of attack is 90% going to be attacking the problem from the syntax level first. Also from long experience, I can tell you that’s a great way to burn yourself out really fast! Instead, start from the bottom of the list above, and get your “workbench” and “tools” squared away first.

Let’s take a closer look at setting up for a home run plugin.

First things first

Now, the very first any programmer worth his or her salt does… is put on some music! A current favorite of mine is Slow Train Soul’s “Illegal Cargo.” If you’re a Hotel Costes fan, you’ve likely heard “In the Black of the Night.” Well, most of the rest of the album is as good as that cut.

Ok, the music is taken care of, for now. Let’s get on with it.

Build your “WAMP” sandbox first

If you’re serious about knocking it out of the ball park, you need to build your own “sandbox.” In computer programming and software development, a sandbox is complete working environment where you can develop new code, experiment and play around with your applications and toolchain without any fear of destroying your product. A sandbox is somewhat like a training field that athletes use for practice before and between games.

Specifically, creating a sandbox means setting up your local computer to act as both a development computer and as a it’s own networked computer. None of this costs any money, but it will cost you some time. Spend that time now, it will pay you back very quickly. I know this because I put this off for 15 years (yes, I was on the web in August 1994).

Plugin sandbox tools

  1. Install a WAMP/LAMP system.
  2. Install a modern IDE like Aptana
  3. Download a few dozen plugins through “Plugin >> Add New.” I recommend Sniplets, Drain Hole, Amazon Reloaded for starters.
  4. Download my WordPress Plugin Starter Kit. My little example plugins will boost your knowledge and productivity tremendously.

Good tools make for easy work

Once you have your sandbox installed, it’s time to consider what sort of programming editor to use. Your choices range from Microsoft’s Notepad, to expensive code editors with more features than anyone could possibly use. I’ve found a nice, middle-ground solution in Aptana Studio. Aptana allows writing and in many cases executing code in a variety of languages including HTML, CSS, PHP and Javascript. Such capability is important in web applications, which often mix several languages in one application.

Aptana is an Eclipse application, which means it shares the same basic functionality and toolkit with Eclipse. Using Aptana gives you leverage if you later want to use Eclipse for Java coding, and vice versa: if you are familiar with Eclipse, Aptana will be easy to use.

Aptana.com has a number of other interesting services, including reasonably priced cloud computing facilities which are not important right now.

Network and server operation

You do not need to know or do much, provided you have a decent host. Once you have your main testing WordPress blog set up on localhost, and can run your final test on your hosted blog (or even better a test blog on your host), WordPress typically makes it pretty easy.

But don’t be afraid to learn as much as you can about server applications such as Apache, Microsoft IIS, MySQL and related technology on your journey to mastery. It will pay you back later when you need to move your blog, fix broken links, create redirections and handle that host of administrative chores that seem to grow with time.

Programming language and syntax

If you have read this far, I’m making the safe assumption you haven’t been scared away yet, and that you’re fairly comfortable with HTML and CSS. Using HTML and CSS is a prerequisite for really digging into WordPress. Fortunately, both are fairly easy to learn. Anyone technically inclined can pick up HTML in a couple of days, and CSS in a couple of weeks. If you haven’t ever hand-coded a web page, you should go do one right now. Do something fancy. Then come back and continue.

As it turns out, PHP is also pretty easy to learn… provided you already understand HTML (the CSS comes in sideways). Again, the best way to learn some PHP is just go code up a simple PHP-driven web page. That should take a day or three, and you will have enough knowledge to dig into reading and writing simple WordPress plugins.

Learning the WordPress Plugin System

The PHP programming language doesn’t have many built-in control on program design, and has garnered a fair amount of contempt over the years as being a language promoting really bad programming. While it’s true that it’s easy to write really awful code in PHP, good software architects can use the strengths of the tool (PHP) to create a programming methodology every bit as robust as can be built with languages with more formal restrictions. And this is what the WordPress programmers have done. The WordPress system is a masterpiece!

The WordPress plugin subsystem is a piece of art… poetry as claimed on wordpress.org.

Once you have learned enough PHP to endanger yourself (that’s a programming joke!), run, don’t walk to purchase Vladimir Prelovac’s “WordPress Plugin Development Beginner’s Guide.”

Now, you need to commit to memory the WordPress definitions of the following terms:

  • event: Something the user does, like save a post, or activate a plugin.
  • action: A function that executes in response to an event.
  • filter: A function that applies itself to WordPress content such as posts and pages.
  • hook: An action function or filter function which is registered with WordPress to be loaded and executed at a precisely defined stage of WordPress execution.

Get these definitions very clear in your head, then go read in more detail about the WordPress Plugin API.

Once you have digested the Plugin API, check out all the excellent tools WordPress gives you for building actions and filters:

After you have read through these documents, you’re ready to take the next step: programming your own plugins!

“Wait!” you say… “It can’t be that easy… I’ve programmed before… you can’t fool me!”

Ok, you’re right, the devil IS in the details… so here are some simple details for you to peruse.

Simple examples demonstrate plugin operation

To make super easy for you to get started programming plugins, I created some very small demonstration examples:

  1. Demo Plugin Paths: PHP has a number of built-in functions for handling paths and file names, which are leveraged by WordPress to handle plugins. In addition, WordPress defines some preset file and path names you need to know to make your plugin programming much more effective. Demo Plugin Paths lists out a set of these… it’s like having an online manual at your fingertips… customized for your own, personal WordPress installation!
  2. Demo Plugin Options: Do your users need to set options? It’s easy and this plugin lays out option setting code, and only option setting code, so you can see exactly how it’s done.
  3. Demo Plugin Database: Do you need a database table? Demo Plugin Database shows you how to add a database table on activation and drop it on deactivation.
  4. Demo Plugin i18n: Make your plugin worldwide using WordPress’s adaptation of the freely available gettext system.

These plugins are currently under development, but you can download Demo Plugin Database right now. Contact me for a current working version of any of the others.

Appendix: Simple SVN for newbies

Once you understand svn checkout, svn update, and svn commit, these three commands here will take your subversion skills to the next level. Actually, it’s not about subversion… learning these three commands will take your software engineering skills up one notch.

  1. svn switch let’s you update your repositories in place.
  2. Branching using svn copy let’s you work on new code, or bug fix old code, in a safe way without destroying previous work.
  3. Merging code changes from svn branch to svn trunk is easy.

Mastering these three commands is simple and will let you leverage your locally hosted WordPress installation fantastically: more programming, less hassle.