FREE THOUGHT · FREE SOFTWARE · FREE WORLD

Home » WordPress » Tips on Writing WordPress Plugins

by 9 comments

Just read a forum topic Organizing Plugins on the wordpress.org 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.

$aa_pluginshortname_data=get_plugin_data(__FILE__);

Conventions

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

<?php
/*
Plugin Name: AskApache Plugin Shortname
Plugin URI: http://www.askapache.com/wordpress-plugins/askapache-plugin-shortname.html
Description: Adds Intelligent SEO Ajax-powered Google Search to 404 pages.  Options configuration panel
Version: 3.0
Author: AskApache
Author URI: http://www.askapache.com
*/
 
/*
== Installation ==
 
1. Upload aa-pluginshortname.zip 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, www.askapache.com                   |
| 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     |
| 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 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="http://www.askapache.com/wordpress-plugins/tip-google-analytics-404-error-page.html">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><a id="aa_pluginshortname_dataName" href="#aa_pluginshortname_dataName">'.$aa_pluginshortname_data['Name'].'</a></h2>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 '<div style="width:400px;margin:20px auto;">
<h3><a id="Info_Google" href="#Info_Google">More Info from Google</a></h3>
<ul>
<li><a href="http://code.google.com/apis/ajaxsearch/">AJAX Search API</a></li>
<li><a href="http://code.google.com/apis/ajaxsearch/signup.html">Start Using the API</a></li>
<li>AJAX Search Wizards</li>
<li><a href="http://code.google.com/apis/ajaxsearch/documentation/">Developer Guide</a></li>
<li><a href="http://code.google.com/apis/ajaxsearch/documentation/reference.html">Class Reference</a></li>
<li><a href="http://code.google.com/apis/ajaxsearch/samples.html">Code Samples</a></li>
<li><a href="http://code.google.com/apis/ajaxsearch/community-samples.html">Community Samples</a></li>
<li><a href="http://code.google.com/support/bin/topic.py?topic=10021">Knowledge Base</a></li>
<li><a href="http://googleajaxsearchapi.blogspot.com/">AJAX APIs Blog</a></li>
<li><a href="http://groups.google.com/group/Google-AJAX-Search-API">Developer Forum</a></li>
</ul>
<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(){
delete_option('aa_plugin_shortname_url');
delete_option('aa_plugin_shortname_code_html');
delete_option('aa_plugin_shortname_code_js');
delete_option('aa_plugin_shortname_code_css');
delete_option('aa_plugin_shortname_api_key');
}

Activation Function

function aa_plugin_shortname_activate(){
global $aa_pluginshortname_data;
$aa_pluginshortname_data=get_plugin_data(__FILE__);
update_option('aa_plugin_shortname_url',$aa_plugin_shortname_url);
update_option('aa_plugin_shortname_api_key',$aa_plugin_shortname_api_key);
update_option('aa_plugin_shortname_code_html',stripslashes($aa_plugin_shortname_code_html));
update_option('aa_plugin_shortname_code_js',stripslashes($aa_plugin_shortname_code_js));
update_option('aa_plugin_shortname_code_css',stripslashes($aa_plugin_shortname_code_css));
}

Tags

Comments Welcome

AskApache is an FSF Contributing Member



My Online Tools
Popular Articles


Hacking and Hackers

The use of "hacker" to mean "security breaker" is a confusion on the part of the mass media. We hackers refuse to recognize that meaning, and continue using the word to mean someone who loves to program, someone who enjoys playful cleverness, or the combination of the two. See my article, On Hacking.
-- Richard M. Stallman









[hide]

It's very simple - you read the protocol and write the code. -Bill Joy

Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution 3.0 License, just credit with a link.
This site is not supported or endorsed by The Apache Software Foundation (ASF). All software and documentation produced by The ASF is licensed. "Apache" is a trademark of The ASF. NCSA HTTPd.
UNIX ® is a registered Trademark of The Open Group. POSIX ® is a registered Trademark of The IEEE.

+Askapache | askapache

Site Map | Contact Webmaster | License and Disclaimer | Terms of Service | HD Moore

↑ TOPMain