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

 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’ 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


[ 404 Error Page tip for Google Analytics - Home: '.$aa_pluginshortname_data['Title'].' - Version: '.$aa_pluginshortname_data['Version'].' - Author: '.$aa_pluginshortname_data['Author'].' ]

'; $aa_main ='


Test Your 404 Page'; _e($aa_head); _e($aa_main);

Using functions to output code blocks

function aa_print_google_links(){
// Thanks Google, you Rock from the Start!
echo '';

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;