Tips on Writing WordPress Plugins

Just read a forum topic Organizing Plugins on the support forums and thought I'd give you all the tips and tricks that I've learned and use when developing WordPress plugins.. which can be quite fun! I'm definately not a php or WordPress expert, anyone can create useful WordPress plugins without being a hacker.

Check out the Source of Plugins

One of the most helpful and interesting ways that I improve my coding and learn new tips and tricks is by checking out the source code to as many interesting plugins that I can. Each plugin author has a completely unique style of coding in most cases, and this can vary from super-advanced like the Google Sitemap Generator plugin to the incredibly-simple like the AskApache Search Engine Verify plugin.

Research with a Purpose

Instead of just picking plugins at random to reverse-engineer, I surf the WordPress plugin repository for unique and interesting plugins that perform a unique function or contain a unique feature. If I wanted to Learn about MySQL search and replace code for wordpress, I would check out the source code of the excellent Search Regex plugin. Or if I wanted to learn about creating static files from dynamic content on my blog and save and serve from a mysql database, I would check out WP-Cache, and some of the translator plugins.

Use a plugin Template

Any real/elite Web Developer knows the extreme value of using modular, templateable code. CSS, XHTML Strict, unobtrusive javascript, PHP, all are geared towards templates.. And of course WordPress is one of the mack-daddies of templates. So why not use a template for plugin files?

The AskApache WordPress Plugin Template

WordPress Plugin Template: AskApache PluginsNotice that each of my plugins have the same look, feel, and functionality from the Manage Plugins page? This isn't coincidence, I am addicted to making my life easier, and that me4ans clean, modular, reusable templatable code. Lets dig in.

My favorite WordPress Plugin Tip

The get_plugin_data function is a simple built-in wordpress function that retrieves values like 'Name', 'Url', etc.. by preg_searching the plugins file. The key part is the very first few lines of a plugin. Do a print_r($aa_pluginshortname_data) and you will see. This is so useful because it means I don't have to hard-code the Plugin Name, URI, Version, Author, etc.. into my code, which makes my code more robust and dynamic.



For each new AskApache plugin I create a folder in the /wp-content/plugins/aa-pluginshortname/ and then in that folder I create the main plugin file with the same name as the folder + .php so the plugin file is /wp-content/plugins/aa-pluginshortname/aa-pluginshortname.php.

aa-pluginshortname.php header stuff

Plugin Name: AskApache Plugin Shortname
Plugin URI:
Description: Adds Intelligent SEO Ajax-powered Google Search to 404 pages.  Options configuration panel
Version: 3.0
Author: AskApache
Author URI:

== Installation ==

1. Upload to the /wp-content/plugins/ directory
2. Unzip into its own folder /wp-content/plugins/aa-pluginshortname/aa-pluginshortname.php
3. Activate the plugin through the 'Plugins' menu in WordPress by clicking "AskApache Plugin Shortname"
4. Go to your Options Panel and open the "AskApache Plugin Shortname" submenu. /wp-admin/options-general.php?page=aa-pluginshortname.php
5. Enter in your google Ajax API Key and hit "Update" Button.
6. Add the code to your 404.php template page by including <?php if(function_exists('aa_plugin_shortname'))aa_plugin_shortname();?> in your main content area.

|                                                                    |
| License: GPL                                                       |
|                                                                    |
| AskApache Plugin Shortname Plugin - brief description               |
| Copyright (C) 2007, AskApache,                   |
| All rights reserved.                                               |
|                                                                    |
| 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     |
| 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 Street, Fifth Floor                                    |
| Boston, MA  02110-1301, USA                                        |
|                                                                    |

Add the Options Menu

add_action('admin_menu', 'aa_plugin_shortname_options_setup');

Setup the Options Page

function aa_plugin_shortname_options_setup() {
global $aa_pluginshortname_data;
add_options_page($aa_pluginshortname_data['Name'], 'AA Plugin Shortname', 8, basename(__FILE__), 'aa_plugin_shortname_page');

Secure the plugin page function with this

if ( function_exists('current_user_can') && !current_user_can('manage_options') ) die(__('Cheatin&#8217; uh?'));
if (! user_can_access_admin_page()) wp_die( __('You do not have sufficient permissions to access this page.') );

Using a default plugin options page header

$aa_head='<p style="text-align:center;">[  <a href="">404 Error Page tip for Google Analytics</a> - Home: '.$aa_pluginshortname_data['Title'].' - Version: <strong>'.$aa_pluginshortname_data['Version'].'</strong> - Author: '.$aa_pluginshortname_data['Author'].' ]</p><hr style="visibility:hidden;">';
$aa_main ='<div class="wrap"><h2 id="aa_pluginshortname_dataName">'.$aa_pluginshortname_data['Name'].' <a class="sl" href="#aa_pluginshortname_dataName"></a></h2>Test Your 404 Page';

Using functions to output code blocks

function aa_print_google_links(){
// Thanks Google, you Rock from the Start!
echo '<div style="width:400px;margin:20px auto;">
<h3 id="Info_Google">More Info from Google <a class="sl" href="#Info_Google"></a></h3>
<li><a href="">AJAX Search API</a></li>
<li><a href="">Start Using the API</a></li>
<li>AJAX Search Wizards</li>
<li><a href="">Developer Guide</a></li>
<li><a href="">Class Reference</a></li>
<li><a href="">Code Samples</a></li>
<li><a href="">Community Samples</a></li>
<li><a href="">Knowledge Base</a></li>
<li><a href="">AJAX APIs Blog</a></li>
<li><a href="">Developer Forum</a></li>
<hr style="visibility:hidden;" />
<hr style="visibility:hidden;" /></div>';

Register the Activation and Deactivation Hooks

register_deactivation_hook(__FILE__, 'aa_plugin_shortname_deactivate');
register_activation_hook(__FILE__, 'aa_plugin_shortname_activate');

Delete Stored options upon deactivation

function aa_plugin_shortname_deactivate(){

Activation Function

function aa_plugin_shortname_activate(){
global $aa_pluginshortname_data;