Leveraging Composer to write a more maintainable Object Orientated WordPress plugin

03/04/2014

As your wordpress plugin grows it can quickly become a nightmare to maintain. To add a feature you just write callback function and wire it into a hook using wordpress’s global functions. Simples! But because it’s so easy, your plugin soon grows into a monolithic class or script, with unrelated functions and features.

WordPress’s core is starting to show it’s age when you compare it to modern PHP practices and tools but we can use these tools to bring some sanity to the maddening rabbit hole your plugin can become.

Here I’ll rewrite Hello Dolly in a more Object Oriented fashion. Full source here.

Step 1 – Create your wordpress plugin file.

Create a folder for your plugin files to reside, I’ve created a folder hello-dolly-oop and inside that a file hello-dolly-oop.php with the appropriate annotations as outlined on wordpress.org.

https://github.com/bgallagher/hello-dolly-oop/blob/5a00c1042d85f5b403a8833601973e72799a32e4/hello-dolly-oop.php

Step 2 – Create your main class

Create a folder src/HelloDollyOOP in the root of your plugin, in here we’ll put all our classes. Our main class we’ll call HelloDollyOOP_Plugin. Save the file as Plugin.php inside the HelloDollyOOP source folderHere we’re using PHP 5.2 style PSR0 underscore namespacing for autoloading as wordpress’s minimum version is still 5.2.x.

Step 3 – Autoloading classes with Composer

We’ll use composer to set up our autoload for us. This means we don’t have to litter our files with require_once statements.

There are lots to great composer articles out there, but basically create a file called composer.json in the root of our plugin.

Then download composer.phar to the root of our plugin and run ./composer.phar update -o . This will create a vendor/composer folder for us containing classes for autoloading.

Now back in our hello-dolly-oop.php file add the line:

This will register the autoloader so that our classes can be found seamlessly.

https://github.com/bgallagher/hello-dolly-oop/commit/eac1868240ed40310f5030858b3c6a73fa1282bd

Step 4 – Instantiating our main plugin class

With our autoloading in place, we can now instantiate our main plugin class in our plugin file.

Here we’re instantiating HelloDollyOOP_Plugin and passing in the location of the plugin file, many WordPress function take this path as an argument, so it’s handy to have a reference to it.

Step 5 – Write a class for Dolly

Next we’ll place the same Hello Dolly logic from the original plugin in a class of it’s own called HelloDollyOOP_Lyrics. Save the file as src/HelloDollyOOP/Lyrics.

https://github.com/bgallagher/hello-dolly-oop/blob/3f4d98edd89a230a79049bc9556dc43cffb18b56/src/HelloDollyOOP/Lyrics.php

Step 6 – Prepare to register the hooks

Now we need to wire the callback functions up.

Create a static method in HelloDollyOOP_Lyrics called registerHooks. In here we’ll wire up the appropiate callbacks.

This way the class itself it responsible for registering it’s callbacks – this is a nice approach because as your plugin grows you can encapsulate all related functionality together.

Step 7 – Register the hooks

Now in the constructor of our main plugin class just call the static method HelloDollyOOP_Lyrics::registerHooks like so:

 

All done. Now you have a good foundation for a plugin that can grow over time.

Share