By Lisa Sabin-Wilson

When you start writing WordPress plugins, you find that you spend a significant amount of time rewriting the same things. Most plugins have the same basic structure and are set up the same way, meaning that they all deal with creating settings pages, storing options, and interacting with particular plugins, among other things. You can save hours of work each time you start a new plugin if you create a template.

Such a template varies from person to person, depending on programming styles, preferences, and the types of plugins to include. If you often write plugins that use your own database tables, for example, you should include tables in your template. Similarly, if your plugins almost never require options pages, leave those pages out of your template.

To create your own template, determine what functionality and structure your plugins usually contain. Then follow these steps:

  1. Create your file structure.As you write more plugins, you’ll find yourself repeating the same general filenames. If you find that you’re including enough JavaScript and Cascading Style Sheets (CSS) in your plugins to necessitate their own files or directories, include them in your template. If you’re using a lot of JavaScript or CSS, you could modify the file structure of your plugin template to look something like the one you see here.wordpress plugin file structure
  2. Determine what functionality you generally have in your plugins.If your plugins usually contain masses of code in a class, you can set up a basic class for your plugin template. Likewise, if your plugins typically have a single options page or a system of top-level and submenu pages, you can set up a general template.
  3. Create your primary plugin .php file.Usually, this file contains some general add_action calls, includes to other files, and other general initializations. If you always call certain actions, set them up in your primary plugin .php file template. If you always register a plugin function to be run when the plugin is activated (register_activation_hook), for example, and add a menu item for the plugin on the Dashboard (admin_menu), add those calls to your primary template, as follows:

    <?php

    $myInstance = new myPlugin();

    add_action('register_activation_hook','my_activation_plugin');

    add_action('admin_menu',array($myInstance, 'admin_menu'));

    ?>

  4. Set up the functions you use most often in the body of your primary plugin .php file.The line of code used here — function my_activation_plugin — was added in Step 3 through the add_action hook. In your plugin template, you define any scripts your plugin uses by adding this function, which fires when a user activates the plugin:

    <?php

    function my_activation_plugin(){

    //plugin activation scripts here

    }

    ?>

  5. (Optional) Create your basic class structure.You might add a few lines of code that resemble the following:

<?php

class myPlugin {

var $options = ;

var $db_version = '1';

function myPlugin() {

add_action('admin_init',array($this,'admin_init');

}

function admin_init(){

//admin initializations

}

function process_options($args,$data){

//process our options here

}

function admin_menu(){

//code for admin menu

}

function __construct(){

//PHP 5 Constructor here

}

} //end class

?>

Obviously, your class template may be more detailed than this example, depending on your particular coding styles and the types of plugins you like to write.

In addition, you may want to set up a basic plugin options page along with plugin options management scripts. Everyone uses different techniques for such things as processing plugin options. When you determine your particular type, include the basic format in your template.

As your programming style, WordPress, or your interest in different types of plugins changes over time, you’ll find that your template needs change, too. Make sure that you update your templates.