Come With Me On My Journey Into Plugin Development

This post will wander. It will ramble.

If you’ve read any of my other posts, you know that rambling is my writing style. But this post will take it to another level. You see, for the last two days – since I opened up my text editor and started writing code – I’ve been all over. I’ve been easily distracted. I’ve explored multi… – squirrel!

If you make it to the end of this post, you’ll see I actually created something. I created a plugin that really has no useful purpose and is beyond over-architected for what it does… but it is a functioning plugin.

Shall we begin?

Let’s Start With A Tutorial Or Two

After looking around for a while, I picked this relatively simple tutorial about using filters to modify post content. I worked through it and it worked fine. I also made this custom post type example work.

I then went back to the filter example and decided to make it “my own” by reducing the features down to simply display a “Follow me on Twitter” at the bottom of every single-post on this site. It was a total of six lines of code.

I Have An Idea!

Somewhere in the middle of experimenting with those plugin tutorials, an idea for a new plugin started to form in my head. This is #squirrelone.

I put the tutorials aside and started mapping out basic functionality and designed a few database tables. I reviewed the WordPress Database Description page first to see if I could find anything native to WP before creating my own tables, but I didn’t see it and I’m not quite ready to tap into core tables.

Given I had designed brand spanking new database tables for my plugin, I decided to start this project by tying into the register_activation_hook() to create my tables on plugin activation.

The Creating Tables with Plugins Codex proved incredibly helpful in this effort and, before too long, I was able to create two tables and insert default data into one of them, simply by activating the plugin. That first successful plugin-install-I-see-the-tables-in-the-DB was definitely an “Oh yeah!” moment. I had a plugin that did something!

But then came #squirreltwo.

I went back to Sublime and looked at my code. It was awful.

I had one file with multiple functions that would only ever be run on activation. I created a plugin that would repeatedly load multiple lines of code that would never be used by end users. Function after function. It was spaghetti code. Well, I actually prefer “cowboy code” – saying that out loud makes me feel a little tougher.

Either way, it wasn’t good. I needed structure. I needed a model to follow to promote clean, extensible code – even if I won’t always write it that way.

Let’s Give OOP A Try

I used to like Object Oriented Programming. I’ve written OOP code to various levels of success over the years, though I don’t think I ever really used polymorphism correctly.

Anyway, I started thinking OOP would be a good way to structure my WP plugin code. I looked around and found this WordPress plugin skeleton built using OOP and the MVC pattern.

Hindsight being 20/20, that may not have been the best resource for me to use with my first custom plugin.

It’s a great resource, but after a while my head just started hurting – abstract classes, inheritance, objects, static functions, protected variables – ugh. I mean, I do like that stuff, but it takes me a while to get back into it and I was having a hard time focusing because it felt like overkill for what I was wanting to do.

Now, I don’t know if you noticed it, but that skeleton code link above also included the letters “MVC.” That gave me another idea.

Enter stage left, #squirrelthree.

MVC Must Be The Answer

I know MVC. At least, I used to.

I’ve spent my fair share of time in the Model-View-Controller architecture, so why not give it a go with my plugin?

I’ll just let you know now that after working with MVC again, I still like it. But I don’t know if it is the “WordPress way.” Am I just that “old guy” trying to hang with the young kids, clueless to how uncool I am? I don’t know.

To give MVC a try, I didn’t switch my new plugin idea over from OOP to MVC. No, I instead went back to my super-simple, 6-line “Follow me on Twitter” plugin and converted it to MVC. Not because it needed so much structure, but because it was a simpler concept to work with.

If you take a quick look at the bottom of this post, or any other post in this site, you’ll see the output of this plugin. It works.

I should mention I followed this MVC Inspired Approach to WordPress Plugin Development very closely. Not exactly the same, but pretty darn close.

Instead of trying to describe what I built, I’ve decided to show you.

Yes, I’ve now journeyed further into this new world of thinking-out-loud-on-the-internet thing I’m doing and created my first Bitbucket repository and placed my Follow Me plugin there for you to review. Please don’t snicker too loudly.

Final Thoughts

I ran into a few PHP errors when trying to load my model and view files. They are not in the same directory as my controller, which caused me some trouble.

I enabled error logging in my dev site and “tail -f” my log file to debug the problem. I’m sure there is a better way to work through errors in WP and I need to figure it out.

I also need to figure out the proper way to load files. The following just seems too messy to get the top-level of my plugin directory and going the “relative” route didn’t work for me:

$plugin_path = $_SERVER[ 'DOCUMENT_ROOT' ] . '/wp-content/plugins/amelung-follow-me/';

Well, that’s it for today.

Happy Memorial Day weekend!