Template for Writing Your First Wordpress Plug-in
It seems like only yesterday, I set out to build my first ever Wordpress Plug-in. Bright eyed and bushy tailed, I poured over the Wordpress Codex, putting together the pieces of the process until I finally got the big picture. The Wordpress Codex is an amazing resource for anyone looking to get into Wordpress development; I strongly recommend any new developer read through the “Writing a Plugin” section.
While learning, I had a difficult time finding a straightforward template I could use as a base for my plug-ins. I wanted something that was clean and well-organized but also provided examples of more complex behaviours, such as: Internationalization, Widgets and Front and Back-End Ajax. I wanted to ensure my plug-in functionality was encapsulated inside a class structure so my functions and variables wouldn’t conflict with the Wordpress Core, or other plug-ins.
I’ve finally gotten around to writing this template plug-in, and I thought I would share it with the community to get your feedback and improvements, and hopefully save new developers some time. If you are interested in learning how to build a plug-in, and have a strong programming background, you can look through my sample code, read the comments and pretty much piece together how everything works. If you want a more thorough description of what is going on, read through this article.
You can download the plug-in from the Wordpress Plugin directory.
Getting Familiar With the Brolly Template Plug-in
- Download the template plug-in.
- Place the files in the plug-ins directory of your development installation of Wordpress.
- Come up with a unique name for your plug-in, such as MyPlugin. Check the Wordpress Plug-in directory to ensure this name hasn’t been used.
- Rename the plug-in folder from B2Template to your plug-in name (i.e. MyPlugin). Do not use spaces or special characters.
- Rename the file B2Template.php to MyPlugin.php
- Rename the file B2Template.class.php to MyPlugin.class.php
- Rename the class inside MyPlugin.class.php from B2Template to MyPlugin
- Rename the constructor function inside MyPlugin.class.php from B2Template() to MyPlugin()
- Activate the plugin from the Wordpress Plugin Administration Panel
- Add the Template widget to your sidebar from the Wordpress Appearance Administration Panel
- View your site, and interact with the widget to observe it’s behaviour
- From the Wordpress Administration page, click on Settings, and select MyPlugin.
- Interact with the plugin from this page, to view its behaviour.
How to Write a Plugin Using the Brolly Template Plug-in
Note: Substitute B2Template in the instructions below, to whatever you named your plugin (i.e. MyPlugin).
There are two main files in the Brolly Wordpress Plug-in Template. The first, B2Template.php, is used to initialize the plugin and route various Wordpress actions and filters to class methods in the second file, B2Template.class.php. Keeping all our plug-in functions inside a class gives us greater flexibility to reuse the code we write for the plugin. It also prevents naming conflicts from occuring when multiple plug-ins use the same function or variable names.