Phoenix: WordPress is the most accepted blogging and CMS platform out there at present. Part of what has made it so booming is its capability to be extended via plugins to execute any function you wish for. Developing a plugin framework is really hard, but WordPress has one of the best solutions seen till date. So how do you get on with taking benefit of it and building your own plugin?
WordPress development is done in PHP, which make it quite accessible as developers usually know their way the language. Building a plugin is just a matter of making a basic file structure and employing WordPress provided functions and even hooks to
WordPress development is done in PHP, which make it pretty accessible as developers generally know their way around the language. Building a plugin is just a matter of creating a basic file structure and using WordPress provided functions and event hooks to actuate your application.
The File Structure
All it takes to building a plugin is a single PHP file, but general practice says that you house your plugin and its files within a directory. To start, create a directory to hold your plugin and give it a name corresponding your plugin. Then, in that directory, create your main PHP file, again naming the file with the name of your plugin:
In this case, I’ve created 3 directories, admin, img, and js to hold my extra files. The super-plugin-admin.php file will be employed to offer a web interface to your plugin on the WP backend.
Lastly, if you plan on hosting your plugin in the WordPress plugin directory, you will need to take in a file named readme.txt at the root of the directory. The file must follow the set-up detailed in this readme.txt example.
Defining Your Plugin
Subsequently, we have to define our plugin so that WP will recognize it and let it to be installed, removed, and activated.
Open your main plugin file, super-plugin.php and add the following to the top of the file:
Plugin Name: Name Of The Plugin
Plugin URI: http://URI_Of_Page_Describing_Plugin_and_Updates
Description: A brief description of the Plugin.
Version: The Plugin's Version Number, e.g.: 1.0
Author: Name Of The Plugin Author
Author URI: http://URI_Of_The_Plugin_Author
License: A "Slug" license name e.g. GPL2
Replace each line with the data for your plugin. This will label your plugin details for WordPress. At a minimum you need the Plugin Name: field for the plugin to be known.
Initializing your program
Now that WordPress identifies your plugin, it’s time to make it do something. This is achieved by employing predefined WordPress functions and hooks. A hook is an event listener that is trigger based on outside events happening. If your program required performing any type of setup, such as database table creation, you can tap into the plugin activation hook like so:
//Do some installation work
Then, when a user activates your plugin, any function that has been registered with the activation hook will be executed.
wp_register_script('super_plugin_script',plugin_dir_url( __FILE__ ).'js/super-plugin.js');
When scripts are being loaded up, your function will get carried out and your script will get registered and queued to be added into the head portion of the HTML.
Finally, you want to in fact run your program. You can do this by listening to various WP hooks and waiting for an apt event that would trigger your code to run. If you want to run your program each time a visitor comes to your site, you can use the ‘init’ or ‘wp_loaded’ action to trigger your code:
In this instance, the super_plugin_init() function will get called on every new request and you can perform whatever logic you need. If your plugin is complex, you can add extra PHP files to put up out your application and just use this initial hook to trigger the full application. A full list of the actions that run during a usual request can be found here.
With these elements ready, you’re off and running with your own WordPress plugin. What you do with it is up to you, but you’re only restricted by your imagination. Uploading your directory to your WordPress installation will make your plugin appear in the plugins section and you can turn on it like any other plugin.
Next up, you’ll probably want some way to administer the plugin on the backend of WordPress. In my next post, I’ll show you how to get started with that process.
Read more: WordPress Plugin Development