This post was written as part of the How to Write a WordPress Plugin series.
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
- 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.
[php]
Ronald Huereca
Description: A sample plugin for a Devlounge series.
[/php]
Shown below is a screenshot of what the plugin would look like in the WordPress Plugins panel.
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.
[php]
if (!class_exists(“DevloungePluginSeries”)) {
class DevloungePluginSeries {
function DevloungePluginSeries() { //constructor
}
}
} //End Class DevloungePluginSeries
[/php]
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.
[php]
if (class_exists(“DevloungePluginSeries”)) {
$dl_pluginSeries = new DevloungePluginSeries();
}
[/php]
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).
[php]
//Actions and Filters
if (isset($dl_pluginSeries)) {
//Actions
//Filters
}
?>
[/php]
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.
Conclusion
The post served as a very basic foundational structure for starting a WordPress plugin. If you want to download the sample code, please feel free. The code will be available after almost every post in this series for you to dig through.
Download the Code Used In This Post
Digging through plugin code is a great way to learn the in’s and out’s of plugin design. I still do it, and I learn something new almost every time. Thank you Lewis for the tip suggestion.
For further reading, please check out these links: