Tips on Writing WordPress Plugins
« A better way to use PDF files onlineReferer Spoofing Using JavaScript »
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.
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.
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.
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?
Notice 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.
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__);
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.
<?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_action('admin_menu', 'aa_plugin_shortname_options_setup');
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');
}
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.') );
$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>'.$aa_pluginshortname_data['Name'].'</h2><a href="http://www.askapache.com/404/test-404-page-plugin-askapache-wordpress-plugin-search-engine.html">Test Your 404 Page</a>'; _e($aa_head); _e($aa_main);
function aa_print_google_links(){
// Thanks Google, you Rock from the Start!
echo '<div style="width:400px;margin:20px auto;">
<h3>More Info from Google</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><a href="http://code.google.com/apis/ajaxsearch/wizards.html">AJAX Search Wizards</a></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_deactivation_hook(__FILE__, 'aa_plugin_shortname_deactivate'); register_activation_hook(__FILE__, 'aa_plugin_shortname_activate');
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');
}
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));
}
« A better way to use PDF files online
Referer Spoofing Using JavaScript »
Please consider donating to support active development of the free software and articles here.![]()
The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect. Tim Berners-Lee
Hi there. Where do you execute get_plugin_data? When I try to execute it it says call to undefined function. To what action hook can I hook to execute it when it is ready?
Great article, thanks for the wonderful tips. Wonder if there is something like this for theme development.
Another good tip. If you have a large part of your plugin code base devoted to the admin stuff, split it into another file. In your main plugin add this in:
if (is_admin())
include(‘plugin-admin.php’);
No need to have PHP read and compile a bunch of functions that are only used by a couple of people on a small percentage of page views.
I been doing this on WP plugins for a couple of years. Drupal also went to a similar system with their modules in D6.
It's very simple - you read the protocol and write the code. -Bill Joy
HTML | DCMI | GRDDL | XOXO | XDMP | XFN | DOM | XML | XHTML 1.1 Strict | CSS 2.1 | W3C | TLDP | WAI | DISA | ICSI | GIAC | SANS RR | GHOST | DEFCON | NIST | DHS CYBER | NIST
↑ TOPExcept 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. HTTPD based on NCSA HTTPd
Great Article for Beginners