Posts from — November 2009
Anatomy of a wordpress plugin
This article is the first in a series of how-to articles which will go through all the steps required to create a wordpress plugin.
At the end of the article, there will be a download link to a working plugin that you can download and use as a template for building your own plugins. The latest version of Wordpress is required (2.8.5), which is the version I used when writing this article. Earlier versions might also work, but I can’t guarantee it.
The first step is to create a new php file called testplugin.php
/*
Plugin Name: Rawseo - test plugin template
Plugin URI: http://www.rawseo.com
Description: A great plugin template
Version: 1.0
Author: Rawseo
Author URI: http://www.rawseo.com
*/
?>
The comment headers in your main plugin file (everything above between (/* and */) give wordpress information about your plugin which is displayed on the plugin information screen.
If you were to upload our template plugin file right now to your Wordpress plugins directory (wp-content/plugins), you would see the following in your plugin manager:

Now, we are going to add a settings page for our new plugin.
/*
Plugin Name: Rawseo - test plugin template
Plugin URI: http://www.rawseo.com
Description: A great plugin template
Version: 1.0
Author: Rawseo
Author URI: http://www.rawseo.com
*/
function checkAuth()
{
return current_user_can('activate_plugins');
}
function settings_subpanel()
{
global $wpdb; //this gives us access to the wordpress database
if (isset($_POST['new_email_address']))
{
update_option("admin_email",$wpdb->escape($_POST['new_email_address']));
}
echo '<div class="wrap">';
echo '<h2>Rawseo test plugin</h2>';
echo '<form action="" method="post">
<p>This plugin allows you to edit and save the wordpress admin email address</p>
Admin Email Address: <input type="text" size="20" name="new_email_address" value="'.get_option("admin_email").'"><br/>
<input type="submit" value="Save" /></form>';
}
function settings_page()
{
if (checkAuth())
{
if (function_exists('add_options_page'))
{
//add_options_page(page_title, menu_title, capability, file, [function])
add_options_page('Rawseo options','Rawseo test plugin','administrator', basename(__FILE__),
'settings_subpanel');
}
}
}
add_action('admin_menu', 'settings_page');
?>
Understanding how to create your own plugin first requires an explaination of some built-in wordpress functions that are required to properly communicate with various sections of your administration panel and blog.
Even though it’s at the end of the file, add_action starts the process rolling for the settings page. Wordpress allows you to hook into different events with your own functions, which can execute and process code at various times. Since we are adding a new settings page onto the administrative menu, ‘admin_menu’ is passed as the first parameter (which is the event) and ’settings page’ is passed as the second (this is our function that will be called for that event).
{
return current_user_can('activate_plugins');
}
CheckAuth() checks to see if the user has the correct permission to view or edit our plugin settings through a function called current_user_can.
The following is a list of all of the possible permissions that you can check:
- install_themes
- update_themes
- switch_themes
- edit_themes
- install_plugins
- activate_plugins
- edit_plugins
- update_plugins
- delete_plugins
- create_users
- edit_users
- delete_users
- edit_files
- manage_options
- import
- unfiltered_upload
- edit_dashboard
- moderate_comments
- manage_categories
- manage_links
- unfiltered_html
- edit_published_posts
- edit_others_posts
- edit_pages
- edit_others_pages
- edit_published_pages
- publish_pages
- delete_pages
- delete_others_pages
- delete_published_pages
- delete_others_posts
- delete_private_posts
- edit_private_posts
- read_private_posts
- delete_private_pages
- edit_private_pages
- read_private_pages
- upload_files
- publish_posts
- delete_published_posts
- edit_posts
- delete_posts
- read
add_options_page adds our new options page to the settings menu.
add_options_page(page_title, menu_title, capability, file, [function])
page-title: Text that will go into the HTML page title for the page when the menu is active.
menu-title: The on-screen name text for the menu.
capability: The minimum role required to display and use this menu page.
examples: Editor,Author,Contributor,Editor,Administrator
file: If the function parameter is omitted, this should be the PHP file that handles the display of the menu page content.
function: function that displays the page content for the menu page

settings_subpanel() displays all of the HTML that will be contained in the new settings page. I also used a couple of built-in functions that may help you when you build your own plugins.
update_option(): Update an option in the wp_options table.
get_option(): Get an option from the wp_options table
$wpdb->escape(): Used to prevent injection attacks by encoding certain characters. You should be ussing this function (or some other type of escaping) on any data sent into a database.
Download
This example plugin will display an input form which will allow you to update the current wordpress admin email address. If you would like to download the plugin example from this article, it can be found here.
November 17, 2009 Comments Off