Introduction
The Wordpress Plugin Framework (WPF) is a PHP class that is used to provide a framework for the development of Wordpress plugins. The overall intention of the WPF is to generalize and simplify plugin design while also helping plugins adhere to a common administration and usage standard. This manual will help guide you in the development and implementation of a Wordpress plugin derived from the WPF base class.
Getting Started
This section will provide a strategy for the creation of a plugin based from the WPF.
Creating a Plugin Package
The first step to developing any Wordpress plugin is to create a plugin package. This package consists of a specially named folder to contain your plugin files and a specially named file that is your main plugin file.
So for our example plugin design we will need to do the following:
Don't worry about writing any code at this point. You will need to download and integrate the WPF before you can begin your true plugin development. Don't worry though... integrating the WPF into your plugin package will only take a few minutes.
Obtaining the Wordpress Plugin Framework
Now that you have created your basic plugin package you can begin integration of the WPF into your plugin package. But first, you will need to download the latest release of the WPF from the Wordpress subversion repository.
Integrating the Wordpress Plugin Framework
Integration of the WPF into your plugin package is a fairly simple and painless process. First you will need to unzip the archive file that you retrieved from the Wordpress subversion repository. Next you will need to copy the wordpress-plugin-framework.php and README.txt files into your plugin package folder.
So for our example plugin design we will need to do the following:
See... I told you it would be painless... well, you aren't quite done yet. You will still need to customize a small section of the wordpress-plugin-framework.php file for your specific plugin.
Customizing the Wordpress Plugin Framework
The only customization required for integration of the WPF with the plugin is to modify the name of the WPF class so that it is specific to the plugin package. This is required due to the lack of namespace support in PHP 4/5.
This step is extremely important so to make sure that you get it right I will walk you through a demonstration of customizing for the My Test Plugin package.
So for our example plugin design we will need to do the following:
class MyTestPlugin_WordpressPluginFramework
You have now successfully customized the wordpress-plugin-framework.php file for your plugin. From this point forward, you will not need to modify this file unless you perform one of the following tasks.
Basic Plugin Implementation
You are now ready to get the ball rolling on your plugin development. However, before we can deep dive the usage of the WPF you will need to perform a few standard Wordpress plugin development steps first.
Standard Wordpress Plugin Requirements
Every Wordpress plugin is required to contain a valid Wordpress plugin header section and a valid README.txt file. Specifics about these two items are beyond the scope of this document. However, you may find more information about these two topics by clicking on the link below.
Creating the Plugin Class
Before you can begin developing your plugin's class you will need to include the WPF in your plugin's file. This is easily accomplished by using the require_once() function.
So for our example plugin design we will need to do the following:
require_once( "wordpress-plugin-framework.php" );
Now that the WPF is included in the project you can safely begin development of the plugin class. The first step in the class development is to derive your plugin class from the WPF base class (i.e. the class that was previously customized for this plugin).
So for our example plugin design we will need to do the following:
class MyTestPlugin extends MyTestPlugin_WordpressPluginFramework
{
// Add class specific code here...
}
This step completes the creation of a plugin derived from the WPF base class.
Instantiating & Initializing the Plugin
Now that the derived plugin class has been created we need to do something with it. First the plugin class will need to be instantiated before it can be initialized. Once it has been instantiated it will need to be properly initialized so that the WPF will understand how to work with this specific plugin. The Initialize() function requires that the developer input the plugin's $title, $version, $subfolderName, and $fileName parameters.
So for our example plugin design we will need to do the following:
if( !$myTestPlugin )
{
// Instantiate the plugin.
$myTestPlugin = new MyTestPlugin();
// Initialize the plugin.
$myTestPlugin->Initialize( 'Test Plugin for the Wordpress Plugin Framework', '1.00', my-test-plugin', 'my-test-plugin', true );
}
A full description of the Initialize() function is provided below.
/**
* Initialize() - Initializes the standard parameter set associated with this plugin.
*
* This function initializes the standard parameter set associated with this plugin so that the plugin
* may be safely integrated into the Wordpress core.
*
* @param string $title The title of the plugin.
* @param string $version The version of the plugin.
* @param string $subfolderName The name of the plugin subfolder installed under the root plugins directory.
* @param string $fileName The name of the plugin's main file.
* @param boolean $debugModeFlag Flag indicating the state of the plugin debug mode.
*
* @return void None.
*
* @access public
* @since {WP 2.3}
* @author Keith Huster
*/
function Initialize( $title, $version, $subfolderName, $fileName, $debugModeFlag )
{
// Code to initialize the plugin...
}
Adding & Registering Plugin Options
The following sections discuss the methods used to add options (persitent data) to the plugin and properly register the plugin options with the Wordpress core.
Adding Plugin Options
Plugin options provide are simply persistent pieces of data that are stored within the Wordpress database. The option values are updates via HTML <input> objects such as textboxes, checkboxes, etc... The WPF currently only supports adding options to the main Wordpress Options database table so discussion of creating and managing database tables is beyond the scope of this document.
To add an option to the plugin you simply need to call the AddOption() function and provide it with the appropriate set of parameters for the specific option that is being added to the plugin.
So for this example a single HTML textbox option named "myTextboxOption" will be added to the $myTestPlugin class. This textbox will default to the text string "Hello!" and will be modifiable by the user. A description of the textbox option that displays "'Simple textbox option for your plugin." will also be provided to the user. The code to implement this option is described below.
$myTestPlugin->AddOption( $myTestPlugin->OPTION_TYPE_TEXTBOX, 'myTextboxOption', 'Hello!', 'Simple textbox option for your plugin.' );
Don't worry if the syntax for the AddOption() function looks confusing at this point. The following documentation for this function should help answer any questions that may be present at this time.
/**
* _AddOption() - Adds an option to the plugin's options array.
*
* This function adds the specified option to the plugin's options array. This array can then be used to
* manage the interface between the plugin options and the Wordpress options database.
*
* @param string $optionType Type of the option to add to the array.
* @param string $optionName Name of the option to add to the array.
* @param mixed $optionValue Value of the option to add to the array.
* @param string $optionDescription Description of the option to add to the array.
* @param string $optionValuesArray Array of selectable values for the option.
*
* @return void None.
*
* @access public
* @since {WP 2.3}
* @author Keith Huster
*/
function AddOption( $optionType, $optionName, $optionValue, $optionDescription, $optionValuesArray = '' )
{
// Code used to add the option...
}
The valid option types that are currently supported by the WPF are listed below.
// Add a radiobuttons group containing three radiobuttons (default to "Radio2" being selected). $myRadioButtonOptions = array( 'Radio1', 'Radio2', 'Radio3' ); $myTestPlugin->AddOption( $myTestPlugin->OPTION_TYPE_RADIOBUTTONS, 'myRadiobuttonsOption', $myRadioButtonOptions[1], 'Simple textbox option for your plugin.', $myRadioButtonOptions );
Registering Plugin Options
Once all of the plugin options have been added to the plugin it is safe to call the RegisterOptions() function to properly register the plugin options with the Wordpress core.
So for our example plugin design we will need to do the following:
// Register the plugin options with the Wordpress core. $myTestPlugin->RegisterOptions( __FILE__ );
Once the plugin options have been successfully registered an administration interface can be added to allow the user to interact with the plugin.
Creating the Plugin Administration Interface
The following sections discuss the methods used to add an administration interface to the plugin utilizing the WPF that is accessible via the Wordpress administration dashboard.
Adding Plugin Administration Blocks
The WPF provides a standardized method of adding content to the plugin administration page. This standard method utilizes the concept of "blocks" of content to help define specialized sections of the plugin's administration page. Each of these "blocks" is displayed as a collapsible section within the plugin's administration page.
To add a block to the plugin's administration page the AddAdministrationPageBlock() function must be called. This function utilizes a defined set of parameters to determine the name, content, and location of the specified page block within the plugin administration page.
The following documentation is provided for the AddAdministrationPageBlock() function to help answer any questions that may be present at this time.
/**
* AddAdministrationPageBlock() - Adds a block of content to be displayed in the plugin's administration page.
*
* This function adds a block of content (i.e. an instance of a dbx-box class) to the plugin's administration
* page. The placement and size of the block is controlled by the $blockType parameter.
*
* @param string $blockId ID of the content block used in HTML formatting (no spaces allowed).
* @param string $blockTitle Title of the content block.
* @param string $blockType Type of content block (one of CONTENT_BLOCK_TYPE_xxx).
* @param string $blockFunctionPtr Function containing the content to be displayed.
*
* @return void None.
*
* @access public
* @since {WP 2.3}
* @author Keith Huster
*/
function AddAdministrationPageBlock( $blockId, $blockTitle, $blockType, $blockFunctionPtr )
{
// Code used to add the page block...
}
The $blockType parameter is specifed to be one of the pre-defined block types listed below.
The final parameter to the AddAdministrationPageBlock() function is a pointer to the function containing the content to be displayed within the page block. This implementation method allows the plugin developer to maintain separation between the plugin being developed and the core WPF functionality. Hence all HTML display functions within the plugin class can be isolated from the inner core functionality of the WPF.
So for this example a single page block that displays "Hello World!" will be added to the plugin administration page. The plugin class function used to contain the raw HTML code for displaying the "Hello World!" text string is defined to be HTML_DisplayPluginHelloWorldBlock().
// Plugin class function containing the raw HTML content code.
function HTML_DisplayPluginHelloWorldBlock()
{
?>
<p>Hello World!</p>
<?php
}
// Function used to add the content block to the plugin administration page. $myTestPlugin->AddAdministrationPageBlock( 'block-hello-world', 'Hello World', $myTestPlugin->CONTENT_BLOCK_TYPE_SIDEBAR, array($myTestPlugin, 'HTML_DisplayPluginHelloWorldBlock') );
Adding "About This Plugin" Links
The Wordpress Plugin Framework automatically creates a SIDEBAR administration page content block that includes links to more information about the plugin including the plugin's homepage, support forum, and donation link. This content block is created by calling the AddAboutThisPluginLinks() function.
So for this example plugin we would need to do the following.
// Add the requested "About This Plugin" web links. $donateLink = 'http://www.mytestplugin.com/donate'; $homepageLink = 'http://www.mytestplugin.com'; $supportForumLink = 'http://www.mytestplugin.com/support'; $myTestPlugin->AddAboutThisPluginLinks( $donateLink, $homepageLink, $supportForumLink );
Registering the Plugin Administration Page
Once all of the content blocks have been added to the plugin administration page and the "About This Plugin" links have been added it is safe to call the RegisterAdministrationPage() function to properly register and display the plugin administration page within the Wordpress dashboard.
The following documentation is provided for the RegisterAdministrationPage() function to help answer any questions that may be present at this time.
/**
* RegisterAdministrationPage() - Registers the plugin's administration page.
*
* This function registers the plugin's administration page with the Wordpress core via the add_action()
* hook. This hook allows the plugin's administration paege to be processed as any standard Wordpress
* administration page (such as the dashboard).
*
* @param string $parentMenu Parent menu of the plugin's administration menu.
* @param string $minimumAccessLevel Minimum user access rights required to access the plugin's administration page.
* @param string $adminMenuTitle Name of the plugin's administration menu.
* @param string $adminMenuPageTitle Browser title of the plugin's administration page.
* @param string $adminMenuPageSlug URI slug displayed for the plugin's administration webpage.
*
* @return void None.
*
* @access public
* @since {WP 2.3}
* @author Keith Huster
*/
function RegisterAdministrationPage( $parentMenu, $minimumAccessLevel, $adminMenuTitle, $adminMenuPageTitle, $adminMenuPageSlug )
{
// Code used to register the plugin administration page...
}
The $parentMenu parameter is specifed to be one of the pre-defined menu items listed below.
The $minimumAccessLevel parameter is specifed to be one of the pre-defined access level items listed below.
So for our example plugin design we will need to add the plugin administration page to the "Plugins" parent menu and we will require "Administrator" level access to this page.
// Register the plugin administration page with the Wordpress dashboard core. $myTestPlugin->RegisterAdministrationPage( $myTestPlugin->PARENT_MENU_PLUGINS, $myTestPlugin->RIGHTS_REQUIRED_ADMIN, 'My Test Plugin', 'My Test Plugin Options Page', 'my-test-plugin-options' );
Once the plugin has been successfully initialized an administration interface has been added and registered the plugin can be safely integrated into a Wordpress installation.
Appendix A: Example "my-test-plugin.php" File
The following code fully implements the My Test Plugin plugin described throughout this document.
<?php
/**
* --------------- DO NOT DELETE!!! ---------------
*
* Plugin Name: My Test Plugin
* Plugin URI: http://code.google.com/p/wordpress-plugin-framework/w/edit/WordpressPluginFrameworkUserManual
* Description: A simple test plugin used to demonstrate the Wordpress Plugin Framework.
* Version: 0.01
* Author: Double Black Design
* Author URI: http://www.doubleblackdesign.com
*
* --------------- DO NOT DELETE!!! ---------------
*
* This is the required license information for a Wordpress plugin.
*
* Copyright 2007 Keith Huster (email : husterk@doubleblackdesign.com)
*
* 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 St, Fifth Floor, Boston, MA 02110-1301 USA
*
* --------------- DO NOT DELETE!!! ---------------
*/
/**
* Include the WordpressPluginFramework.
*/
require_once( "wordpress-plugin-framework.php" );
/**
* MyTestPlugin - Simple plugin class used to demonstrate the WordpressPluginFramework.
*
* @package my-test-plugin
* @since {WP 2.3}
* @author Keith Huster
*/
class MyTestPlugin extends MyTestPlugin_WordpressPluginFramework
{
/**
* HTML_DisplayPluginHelloWorldBlock() - Displays the "Hello World!" content block.
*
* This function generates the markup required to display the specified content block.
*
* @param void None.
*
* @return void None.
*
* @access private Access via internal callback only.
* @since {WP 2.3}
* @author Keith Huster
*/
function HTML_DisplayPluginHelloWorldBlock()
{
?>
<p>Hello World!</p>
<?php
}
}
/**
* Demonstration of creating a plugin utilizing the WordpressPlugin Framework.
*/
if( !$myTestPlugin )
{
// Create a new instance of My Test Plugin class and initialize the plugin.
// NOTE: Initialize the plugin in "Debug Mode".
$myTestPlugin = new MyTestPlugin();
$myTestPlugin->Initialize( 'Test Plugin for the Wordpress Plugin Framework', '1.00', my-test-plugin', 'my-test-plugin', true );
// Add the plugin options and initialize the plugin.
$myTestPlugin->AddOption( $myTestPlugin->OPTION_TYPE_TEXTBOX, 'myTextboxOption', 'Hello!', 'Simple textbox option for your plugin.' );
$myTestPlugin->RegisterOptions( __FILE__ );
// Add the administration page content blocks and register the page.
$myTestPlugin->AddAdministrationPageBlock( 'block-hello-world', 'Hello World', $myTestPlugin->CONTENT_BLOCK_TYPE_SIDEBAR, array( $myTestPlugin, 'HTML_DisplayPluginHelloWorldBlock' ) );
// Add the requested "About This Plugin" web links.
$donateLink = 'http://www.mytestplugin.com/donate';
$homepageLink = 'http://www.mytestplugin.com';
$supportForumLink = 'http://www.mytestplugin.com/support';
$myTestPlugin->AddAboutThisPluginLinks( $donateLink, $homepageLink, $supportForumLink );
// Register the plugin administration page with the Wordpress core.
$myTestPlugin->RegisterAdministrationPage( $myTestPlugin->PARENT_MENU_PLUGINS, $myTestPlugin->RIGHTS_REQUIRED_ADMIN, 'My Test Plugin', 'My Test Plugin Options Page', 'my-test-plugin-options' );
}
RIGHTS_REQUIRED_ADMIN
should be replace by
ACCESS_LEVEL_ADMINISTRATOR
AddAboutThisPluginLinks?() doesn't seem to be part of the Wordpress Plugin Framework v0.04!
You have bad links throughout this entire project. You keep linking to the edit screen, which fails unless the viewer is a member of this group. Thus people just looking at the system can't use them.
Sign in to add a comment
$myTestPlugin->Initialize( 'Test Plugin for the Wordpress Plugin Framework', '1.00', my-test-plugin', 'my-test-plugin', true );
is missing a single quote:
$myTestPlugin->Initialize( 'Test Plugin for the Wordpress Plugin Framework', '1.00', 'my-test-plugin', 'my-test-plugin', true );