Using a Plugin Template

When you start writing WordPress plugins, you find that you spend a significant amount of time rewriting the same things. Typically, most plugins have the same basic structure and are set up the same way, meaning that they all deal with settings pages, storing options, and interacting with particular plugins, among other things. You eventually conclude that a valuable use of your time is to create a template for all the plugins to save you hours of work each time you start a new plugin.

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

image When making a template, keep in mind your personal plugin preferences and tendencies.

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

  1. Create your file structure.

    As you write more plugins, you find yourself repeating the same general filenames. If you find that you're including enough JavaScript and CSS in your plugins to necessitate their own files or directories, include these in your template. For example, 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 shown in Figure 7-1.

    Figure 7-1: Recommended file structure for a plugin.

    image

  2. Determine what functionality you generally have in your plugins.

    If you 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 just contains some general add_action calls, file includes, and other general initializations (check out Chapter 5 of this minibook for information on add_action calls and other plugin functions). If you always call certain actions, like registering a plugin function to be run when the plugin is activated (register_activation_hook) and adding a menu item for the plugin in the Dashboard (admin_menu), set them up in your primary plugin PHP file template.

    <?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, after you add them in Step 3.

    The line of code used here: function my_activation_plugin, was added in Step 3 through the use of the add_action hook. In your plugin template, you define any scripts your plugin uses by adding this function, which then fires when a user activates the plugin:

    <?php
    
    function my_activation_plugin(){
    //plugin activation scripts here
    }
    ?>
  5. (Optional) Create your basic class structure.

    To do this, 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 that, depending on your particular coding styles and the types of plugins you like to write.

In addition to these steps, you might 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, and after 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 will find that your template needs change, too. Make sure that you update your template.

Programmers like to spend their efforts solving problems, writing beautiful code, and achieving awesome functionality. Doing grunt work repeatedly to set up the same basic structure in every plugin takes our energy away from writing beautiful code poetry. Practice these concepts and watch your coding efficiency improve for all your future plugins!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.94.251