..with due apologies to Mark Pilgrim who prefers diving in, we hear :)
WordPress beleives in keeping it simple, and a lot of thought goes into including new functionality as a part of the standard WordPress code (or the 'core' of WordPress), in order to keep WordPress fast and lightweight. Users can extend the functionality provided by WordPress by using plugins. You can also write your own plugins, to fill in the gaps that you see, or to do that special thing with your blog. This article will guide you through the process, explaining the basics and going over a few examples.
So, to begin with, let us define a plugin, shall we?
That said, not every file with a few functions in it can call itself a WordPress Plugin. The following are essential requirements of a WordPress Plugin:
Requirements:
In addition to these requirements will see some recommended practices and standards later in this document. Next, let us look at the WordPress Plugin API
WordPress provides access points for your plugins. These access points, also known as hooks, can be used by plugins to extend the base functionality.
A WordPress Plugin file should have the following structure :
<?php /* Plugin Name: Name_Of_The_Plugin Plugin URI: URI_Of_Page_Describing_Plugin_and_Updates Description: A_Brief_Description_Of_The_Plugin Version: The_Plugin's_Version_Number Author: Name_Of_The_Plugin_Author Author URI: URI_Of_The_Plugin_Author */
You may also wish to add a GPL template, to explicitly mention that the plugin is released under the same licence as WordPress.
/* Copyright 2004 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 as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
These two sections can then be followed by the function definitions and the actual plugin code.
If the Plugin consists of more than one file, then the above comments must be placed at the beginning of the .php file which will reside in the wp-content/
directory. This file usually has the "main" functions, and include
s or require
s the other files used by the Plugin.
There are two types of hooks:
Based on the type of hook used, plugin functions can be classified into two categories:
You can use a filter hook by using the add_filter() function provided by WordPress.
add_filter('filter_hook', 'filter_function_name' [,priority]);
where,
filter_hook
is a Filter Hook provided by WordPressfilter_function_name
is the name of the function that you want to use on the content specified by filter_hook
. This can be a standard php function, a function present in the WordPress core, or a function defined by you in the plugin file.priority
is an optional integer argument that can be used to specify the relative priority with which filters must be applied. If you do not specify this argument, the default priority of 10 is assumed. A filter with a priority of 1 is applied before a filter of priority 2, so the smaller the integer, the higher the priority.Example usage :
Using the following line of code in a plugin:
add_filter('the_title', 'strtoupper', 9);
Will make all the characters in the title of all posts to their upper case characters, by using the PHP function strtoupper
Likewise, you can also remove filters added by WordPress by default if you so desire, by using the remove_filter()
function, which is identical in syntax to the add_filter()
function, as shown below:
remove_filter('filter_hook', 'filter_function_name' [,priority]);
You may want to remove the filters WordPress applies by default to ensure that your own filter plugin works properly, or to stop WordPress from modifying content in a specific manner.
Some of the template tags available in WordPress, which return or echo some form of data or content are Filter Hooks. The following is a list of the filter hooks to format and process data before presenting it to browser:
You can use an action hook by using the add_action() function provided by WordPress.
add_action('action_hook', 'action_function_name' [,priority]);
where,
action_hook
is an action hook provided by WordPress. Some action hooks pass you an argument that you can use in the 'action_function_name' function.action_function_name
is the name of the function that you want to be executed following the event specified by the action_hook
. This can be a standard php function, a function present in the WordPress core, or a function defined by you in the plugin file.priority
is an optional integer argument that can be used to specify the relative priority with which actions must be executed. If you do not specify this argument, the default priority of 10 is assumed. A filter with a priority of 1 is applied before a filter of priority 2, so the smaller the integer, the higher the priority.Example usage :
Using the following line of code in a plugin:
add_action('publish_post', 'sleep(5)', 9);
Will result in WordPress going to sleep (doing nothing) for 5 seconds, after a new post is published, by using the PHP function sleep
Likewise, you can also remove actions executed by WordPress by default if you so desire, by using the remove_action()
function, which is identical in syntax to the add_filter()
function, as shown below:
remove_action('action_hook', 'filter_function_name' [,priority]);
You may want to remove some actions WordPress applies by default to ensure that your own action plugin works properly, or to stop WordPress from modifying content in a specific manner.
The following is a list of the available action hooks:
You may need to store variables that remain after your script ends (options, for example.) WordPress allows you to define, update and get any option, including your own. The functions that enable you to do this are listed below.
As with your functions, make sure your option names do not conflict with other values WP may be using. A good way to do this is to prefix them with a sufficiently unique string, such as your program's short name or your initials.
Add option: add_option($name, $value, $description, $autoload)
(Describe what happens in case of naming conflicts here. Overwrite?)
Get option: get_option($option)
Update option: update_option($option_name, $newvalue)
Want to make functions available to use in templates?All plugin functions are available to be called from templates by default. Simple.
Want an admin screen? Use add_options_page($page_title, $menu_title, $access_level, $file)
.
Somewhere in your plugin, add this function:
function your_function_name_here() { if (function_exists('add_options_page')) { add_options_page('optionspagetitle', 'optionspagename', minuserlevel, basename(__FILE__)); } }
In the last few lines (when adding hooks), add:
add_action('admin_head', 'your_function_name_here');
To check if a plugin is being loaded in an admin page, use the boolean is_plugin_page()
.
To make a form-displaying script process itself, check for form variables and, if they're available, call the processing function.
To check if a page is being requested as a result of a form action, one way is to use a hidden field in the form (named 'submitted', for example). Then in the first lines of your PHP script, use either isset($foo)
, which returns TRUE if a variable has been initialized, or is(empty($foo))
, which is almost the same thing (see PHP documentation for the distinction).
Form with hidden element:
<?php echo ' <form name="example" action="' . $_SERVER[PHP_SELF] . '" method="post"> Text: <input type="text" name="text" /><br /> <input type="hidden" name="submitted" /> <input type="submit" value="Go!" /> <form> '; ?>
To check for your hidden form element:
isset($_POST['submitted']) /* For method=POST */ isset($_GET['submitted']) /* For method=GET */ isset($_REQUEST['submitted']) /* For either type of form */
Alright, so here we go...before you left, mother asked you to remember the following:
Databases are exceptionally good at fetching data and giving it to you, and these operations are (usually) lightning quick. Making changes to the database, though, is a more complex process, and computationally more expensive. As a result, try to minimize the amount of writing you do to the database. Get everything prepared in your code first, so that you can make only those write operations that you need.
Even though databases fetch data blindingly fast, you should still try to reduce the load on the database by only selecting that data which you need to use. If you need to count the number of rows in a table don't SELECT * FROM
, because all the data in all the rows will be pulled, wasting memory. Likewise, if you only need the post_id and the post_author in your plugin, then just SELECT
those specific items, to minimize database load.
Remember: hundreds of other processes may be hitting the database at the same time. The database and server each have only so many resources to spread around amongst all those processes. Learning how to minimize your plugin's hit against the database will ensure that your plugin isn't the one that is blamed for abuse of resources.