WordPress is currently the most popular CMS in use on the internet, it was first released on May 27th 2003 and has had well over 65 million downloads to date, and for that reason alone, getting involved in WordPress development is a good choice.
Don’t Hack the Core!
A while back, if you wanted to create some custom functionality within a CMS system you would need to “Hack the Core”, hacking the core is BAD! Really really bad! The main reason is because of updates, if you add some amazing new functionality to WordPress, those changes will be overwritten if the files you have updated are also being updated in the new WordPress patch.
Another reason is that finding these changes can be very difficult to find when you have been working on a WordPress project for more than a few months.
The way WordPress (and other great popular CMS systems like Drupal) works is by using a “Hooks System”, as a plugin developer you simply add your own files and “hook into” existing WordPress hooks that are fired a certain points. Doing it this way is much more elegant, since these files will not be overwritten when WordPress is updated, and all of your changes will be in one place and easy to find. It also makes sharing your plugins much easier.
Files and File Headers
Once you have a copy of WordPress installed the next step is to create the files for your plugin. The files need to be placed inside the wp-content/plugins folder, these files can either be single files or you can create sub-folders to hold all your plugin files. Personally I find that creating a folder is a much better approach, mainly because even though your plugin may currently be one file, it may grow into multiple files and keeping them together is much tidier.
So, the first course of action is to create a sub-folder and a file to hold our plugin code, this is the structure I created for our example plugin.
wp-admin\
wp-content\
–plugins\
—-footer-fun\
——footer-fun.php
–themes\
wp-includes\
Once the footer-fun.php file has been created and placed inside your plugins directory, you need to setup the header of the file, this is so that WordPress can see your plugin and allow it to be activated and deactivated, the header of the plugin looks as follows :-
footer-fun.php
<?php
/*
Plugin Name: Footer Fun!
Plugin URI: https://forlinux.co.uk
Description: Simple example of how to create a WordPress plugin!
Version: 0.1
Author: Richard Bagshaw
Author URI: https://forlinux.co.uk
License: GPL2
*/
?>
This header is pretty self-explanatory, and is needed so that the plugin is visible in the WordPress Plugin Administration section, from within this section you will now be able to enable and disable the plugin. If you make follow this article and create this example plugin, be sure to activate it from the administration section.
It is also common to place the full license information within the header, for this example we won’t but this would usually look like this.
<?php
/* Copyright YEAR PLUGIN_AUTHOR_NAME (email : PLUGIN AUTHOR EMAIL)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
?>
At this point nothing will happen when you do this, so lets add some code to play with the footer.
Hooks
WordPress uses hooks, using these hooks we can add our own functions which will execute when these hooks are called. There are two types of Hooks available for us to use :-
- Action Hooks
- Filter Hooks
Action Hooks are predetermined points that we can hook into, we do this by adding the following code :-
add_action(‘wp_footer’, ‘footer_extra_stuff’);
This code tells WordPress that we want to attach our own custom function called footer_extra_stuff() to the hook named wp_footer, not surprisingly the wp_footer hook is fired when the footer displays on the website.
Our own custom function is very simple by example and would look something like this :-
function footer_extra_stuff() {
echo ‘This is extra content in the footer’;
}
This is literally as simple as it gets, every time WordPress displays the footer it will fire the wp_footer action hook – and since we have added our own action to this hook our code will execute.
Obviously there are many many more hooks available in WordPress which are either Action Hooks or Filter Hooks, the example given here is an Action Hook.
Action Hooks are hooks that are fired at specific points during execution, or when a specific event occurs. Filter Hooks are used to modify content before it is presented on the screen or posted to the database.
For more information on WordPress Plugin Development and the related APIs/Hooks take a look at the following URLs :-
http://codex.wordpress.org/Writing_a_Plugin
http://codex.wordpress.org/Plugin_API
http://codex.wordpress.org/Plugin_API/Action_Reference
http://codex.wordpress.org/Plugin_API/Filter_Reference