Structure of a WordPress Plugins

One of the more important aspects of developing a WordPress plugin is how you structure it. This post will go over some tips on how to structure your plugin to organize your plugin resources and avoid naming collisions. Each plugin author is different in the way they structure a plugin, so these tips are merely my own personal preference. I’ll first briefly describe how a WordPress plugin works and then go into a plugin’s structure.

How a WordPress Plugin Works:

After placing a WordPress plugin into the “wp-content/plugins/” folder, the plugin should automatically be available to install.

When a plugin is “Activated”, this tells WordPress to load your bit of code on “each” page (including admin pages). This is why if you have many plugins activated, your WordPress installation may be very slow due to the amount of code being included. Since WordPress loads your code automatically when the plugin is activated, you can take advantage of this by tapping into the WordPress Plugin Application Program Interface (API). You can also access the WordPress template tags or create your own.
I suggest reading into the WordPress loop if you plan on making changes to the post content or comments. The WordPress loop is the loop that displays your posts. Some template tags will not work outside of this loop, so it is imperative that you know exactly where your code is executing. You can control this by taking advantage of actions and filters, which will be explained in later posts.

Folder Structure:

All WordPress plugins will be installed in the wp-content/plugins directory. Some plugin authors simply include a PHP file for their plugin, but I recommend always creating a folder to store your plugin.

I typically structure my plugin in this folder structure:

Plugin Folder Name (The name of your plugin with no spaces or special characters)

-> Main plugin php file
-> js folder (for JavaScript files)
-> css folder (for StyleSheet files)
-> php folder (for other PHP includes)

For example purposes, here is a sample structure I have created:

-> devlounge-plugin-series.php
-> js
-> css
-> php

Within the devlounge-plugin-series folder, I would include just the main PHP file and put all other files in their respective folders. This structure will assist other plugin authors who look at your code to be able to tell what the main plugin file is and where all the supporting files are located.
WordPress also recommends placing images in their own directory and including a read  me file for your plugin.

Main Plugin File:

When you start a new plugin file, the first seven lines are the lines that describe your plugin.
1.  <?php
2.  /*
3.  Plugin Name: Your Plugin Name Here
4.  Plugin URI: Your Plugin URI
5.  Version: Current Plugin Version
6.  Author: Who Are You?
7.  Description: What does your plugin do?

Line 3 allows you to name your plugin. Line 4 allows you to point a user to the web location of your plugin. Line 5 allows you to specify the current version. Line 6 allows you to specify the author of the plugin. Line 7 allows you to describe your plugin.

Shown below is an example of the code filled out:
1.  <?php
2.  /*
3.  Plugin Name: Devlounge Plugin Series
4.  Plugin URI:
5.  Version: v1.00
6.  Author: <a href=””>Ronald Huereca</a>
7.  Description: A sample plugin for a <a
href=””>Devlounge</a&gt; series.

Set Up a Class Structure:

You don’t have to be incredibly familiar with PHP Classes to develop a WordPress plugin, but it sure helps. A class structure is necessary in order to avoid naming collisions with other WordPress plugins. If someone out there sets up the same function name as yours in a plugin, an error will result and WordPress will be rendered inoperable until that plugin is removed. To avoid naming collisions, it is imperative that all plugins incorporate a PHP class structure. Here is some bare-bones code that will allow you to set up a class structure.

1. if (!class_exists(“DevloungePluginSeries”)) {
2.     class DevloungePluginSeries {
3.        function DevloungePluginSeries() { //constructor
5.        }
7.     }
9. } //End Class DevloungePluginSeries

What the above code does is checks for the existence of a class named DevloungePluginSeries. If the class doesn’t exist, the class is created.
Initialize Your Class The next bit of code will initialize (instantiate) your class.
1. if (class_exists(“DevloungePluginSeries”)) {
2.     $dl_pluginSeries = new DevloungePluginSeries();
3. }

All the above code checks for is if the class DevloungePluginSeries has been created. If it has, a variable called $dl_pluginSeries is created with an instance of the DevloungePluginSeries class.

Set Up Actions and Filters:

The next bit of code sets up a place holder for WordPress actions and filters (which I will go over in a later post).
1. //Actions and Filters
2. if (isset($dl_pluginSeries)) {
3.     //Actions

5.     //Filters
6. }
8. ?>

The above code checks to make sure the $dl_pluginSeries variable is set. If it is (and that’s only if the class exists), then the appropriate actions and filters are set up.

    • Jenny Stepman
    • November 10th, 2010

    Damn, indeed interesting topic. How can I get this RSS?

    Jenny Stepman
    security shop

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: