WordPress Plugin Construction for the Non-Programmer: Part II


This is a follow up post to my previous post “How to Create a Very Basic WordPress Plugin.” I’d like to follow up on that post by covering some more basic principles for creating WordPress plugins.

You won’t need any programming experience for this, but if you don’t have any, I suggest you also read the first tutorial in this series.

Let’s knock out a few disclaimers…

Five Official “Cover my Butt” Statements

  • At the time of this writing, the most up to date WordPress version is 3.4.1 It might be different by the time you read this .
  • I’m assuming you already know how to install a basic WordPress site, if you’re unsure please see their www.wordpress.org.
  • To any experienced programmers reading, I’m aware that there are many other important things the reader will need to know in order to create live plugins. This is strictly for the people who are starting from ground zero and need to get some traction.
  • Please do NOT attempt anything in these tutorials on one of your live WordPress sites. Rather, set up a subdomain and a brand new WordPress installation to use for plugin testing purposes only.
  • If you’re confused by any of the code or terminology above, please go back and read the first post in this series.

Recap of the Basic WordPress Plugin Structure

Below is the code for the WordPress plugin named “Basic” which we created in the first tutorial of this series. Only this time, I’ve added some additional information to the description (I’ve put the new stuff in italics):

<?php

/*/

Plugin Name: Basic

Plugin URI: www.yourwebsiteurl.com

Description: Demonstrates how a WP plugin works and how to create a unique class for your WordPress plugin.

Version: 101

Author: Your Name

Author URI: www. yourwebsiteurl.com

/*/

function basic_content_replace ($text) {

$text = str_replace(‘Old’,’New’,$text);

return $text;

}

function basic_content_replace ($text) {

$text = str_replace(‘Old’,’New’,$text)

return $text;

}

add_filter(‘the_content’,’basic_content_replace’);

add_filter(‘the_title’,’basic_content_replace’);

Recap of the Basic WordPress Plugin Structure

(Code from basic plugin ends here)

In this tutorial, we’re going to scrap all the code below the “/*/” characters and start with some new lines of code which every WordPress plugin should have.

First, Create a “Toolbox” for Your WordPress Plugin

You’ve probably heard some talk about “object oriented programming.” If you’re just learning about PHP, you might be intimidated by the idea of writing objects. Don’t be. It’s actually easier to use objects than to write individual operations. It’s just hard to find simple explanations of objects, and much of the programming literature makes it harder than it has to be.

I think of an object as a toolbox.

An object is wrapped in a package which we call a “Class” and that class contains functions (we talked about functions in the first tutorial of this series). What confuses most people is the terminology. When a function is used inside a class, it’s called a “method.”

But if you think of the class like a tool box, and the functions (methods) like the tools, it’s a lot more practical and easier to get your brain wrapped around.

So to create an object oriented WordPress plugin, you create the toolbox (the class) and then you put the tools in it (methods/functions). The challenge with writing classes for your WordPress plugin is that your class needs a name. But since it’s sharing the same workspace (WordPress) with other plugins, there’s a chance that another plugin will have a toolbox with the same name as yours.

This could create a problem between your plugin and WordPress or other plugins installed on the WordPress site it’s operating on.

Just imagine being in the “WordPress Workshop” and having someone go tell you to fetch the toolbox (class) named “Basic_tools.” If there were two tools boxes with that name, you’d need more information to get the job done.

In programming, this confusion results in error messages and bugs that can be hard to detect. So before you create your tool box and start putting tools into it, you need to get this naming problem solved. Here’s the new code that will get that done (the new code starts below the “/*/” characters):

<?php

/*/

Plugin Name: Basic

Plugin URI: www.yourwebsiteurl.com

Description: Demonstrates how a WP plugin works and how to create a unique class for your WordPress plugin.

Version: 101

Author: Your Name

Author URI: www. yourwebsiteurl.com

/*/

// check for existing class

if(!class_exists(“YourBrandWPBasic”)){

class YourBrandWPBasic {

functionYourBrandWPConstructor {

}

}//close check for existing class

(end of new code)

Notice that we’ve left some notes for ourselves at key points by using the “//” in order to keep track of what the code is doing. First, we used an if statement along with the “class_exists” function to check whether or not a toolbox (class) named “YourBrandWPBasic” exists. The “!” is a negation operator, it stands for the word “no” or in some cases “not.”

So if you were to translate this line into English…

if(!class_exists(“YourBrandWPBasic”)) {

}

…it would read: “Hey WordPress, if no class exists with the name of “YourBrandWPBasic,” I want you to do what comes in between { and }.

In essence, you’re sending a message through “WordPress Workshop,” to see whether a toolbox named “YourBrandWPBasic” exists. Now, look at what happens in between the { and the } characters:

// check for existing class

if(!class_exists(“YourBrandWPBasic”)){

class YourBrandWPBasic {

functionYourBrandWPConstructor {

}

}//close check for existing class

Notice that we’ve used the keyword “class” followed by the name YourBrandWPBasic. That’s how simple it is to create a tool box with the name that you want. Notice also that we’ve followed the toolbox name up with another set of {} brackets. These characters will simply surround the contents of the toolbox.

Remember, the methods (functions) inside the toolbox are like the tools. Can you see what the name of our first tool is going to be? It’s inside the {} characters right after the word “function.”

So if I translate this entire set of code here into plain English commands…

// check for existing class

if(!class_exists(“YourBrandWPBasic”)){

class YourBrandWPBasic {

functionYourBrandWPConstructor {

}

}//close check for existing class

…it would read like this:

“Attention WordPress Workshop, if no toolbox named “YourBrandWPBasic” exists, I’d like to name my toolbox that and then I’d like to put a tool into it called “YourBrandWPConstructor.”

You might be wondering what happens if the toolbox named “YourBrandWPBasic” already exists. My advice is to brand your class so that this is nearly impossible. For example, notice how our class starts with the words “YourBrand.” I would suggest putting something which is brand specific there, like your name or your company name.

The more brand specific you make your class name, the less likely you are to run into naming conflicts with other classes created by other WordPress plugin developers. So now the little magic elves in the WordPress Workshop will know exactly where to find the tools for your plugin.

Second, Assign a “Handler” to Your Toolbox

Now that you’ve got a toolbox in the WordPress Workshop, you need to assign it to a worker. This worker will be your “handler.” For example, let’s look at the code below (which will go directly below the “}//close check for existing class” notes which we’ve already created:

If(class_exists(“YourBrandWPBasic”)) {

$your_brand_wp_basic = new YourBrandWPBasic();

}

Notice that I set the variable “$your_brand_wp_basic” to equal a new occurrence of the YourBrandWPBasic” class which we created. The “$your_brand_wp_basic” is called the “handler,” in PHP programming, but I like to think of it as the worker who will be working with your Toolbox. You see, a class does nothing by itself, just as a box of tools does nothing by itself.

You have to assign a “handler” to work with your class just as you’d have to assign an elf in the WordPress Workshop to work with your new toolbox. This assignment of a handler to a class is called an “instance” in programming terminology. So if I translate this entire set of code here into plain English commands…

If(class_exists(“YourBrandWPBasic”)) {

$your_brand_wp_basic = new YourBrandWPBasic();

}

…it would read like this:

“Attention WordPress Workshop, if the toolbox named “YourBrandWPBasic” exists, I would like to assign “$your_brand_wp_basic” as the official handler for that toolbox.”

Now you have a toolbox in the WordPress Workshop, and a magic WordPress elf to work with it. All he needs is some commands to work with…and perhaps some more tools.

We’ll be getting into those basics in the next tutorial.

For now, see if you can use what you learned in the last tutorial and in this one to put a few basic tools in your new toolbox and get your WordPress elf to do something to your WordPress testing site. I’ll cover how to do this in the next tutorial so you can see how you did.

Comments and questions are welcome.

-Till next time, Seth C