How to create a WordPress plugin

(At the time of this writing, last stable WordPress version was 4.4.2)

In the plugins folder (normaly at /wp-content/plugins) create a new folder with your plugin name. The name should be unique because all WordPress plugins exist in the same folder. If your plugin file name is too generic, you run the risk of another plugin having the same file name, which would be an obvious problem.
It’s also a good idea to maintain a clean folder structure.

Folder struture example:

…/plugins/your-plugin-name (no spaces or special characters)

your-plugin-name.php — Primary plugin PHP file
uninstall.php — The uninstall file for your plugin
/js — Folder for JavaScript files
/css — Folder for style sheet files
/includes — Folder for additional PHP includes
/images — Folder for plugin images
Creating the Plugin Header

A requirement for all WordPress plugins is a php file with your plugin name (your-plugin-name.php) in the root of your plugin folder with a valid plugin header. The plugin header must be defined at the very top of your main PHP file as a PHP comment.

Following is an example of a standard plugin header:

<?php
/*
Plugin Name: Halloween Plugin
Plugin URI: http://example.com/wordpress-plugins/halloween-plugin
Description: This is a brief description of my plugin
Version: 1.0
Author: Michael Myers
Author URI: http://example.com
License: GPLv2
*/
?>

The only required line in the plugin header is the Plugin Name. The rest of the information is optional but highly recommended.

Plugin License

When developing a plugin you plan on releasing to the public, it’s customary to include the software license that the plugin is released under just below your plugin header (this is not a requirement for the plugin to function).

Following is a standard GPL license, under which most WordPress plugins are released:

<?php
/* Copyright YEAR PLUGIN_AUTHOR_NAME (email : PLUGIN AUTHOR EMAIL)
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, 
or see http://www.gnu.org/licenses/
*/
?>

Since plugins are dependent on WordPress to function, they should also be released under a GPL, or compatible, software license. For more information on GPL licensing visit
http://www.gnu.org/licenses/licenses.html.

Activating and Deactivating Functions

You will need two more important functions. One that is executed when the plugin is activated, and one that is executed when the plugin is deactivated.
For this you must use the register_activation_hook() and register_deactivation_hook() functions. The first is invoked whenever a plugin is activated while the second whenever a plugin is deactivated.
These functions accept two parameters: the location of the main plugin file and a function to execute when the plugin is activated/deactivated respectively.
One important check you should do always when your plugin is activated is to verify when the version of WordPress the user is running is compatible with your plugin.

<?php
register_activation_hook(__FILE__ , 'myplugin_install');

function myplugin_install()
{
    global $wp_version;
    if (version_compare($wp_version, '3.5', '<'))
    {
        wp_die('This plugin requires WordPress version 3.5 or higher.');
    }
}
?>

The preceding function uses the global variable $wp_version, which stores the currently running version of WordPress, and verifies that it is not running a version lower than 3.5. You do the version comparison using the version_compare() PHP function.

There is also a function that executes when the plugin is deactivated called register_deactivation_hook().

<?php
register_deactivation_hook(__FILE__, 'myplugin_deactivate');

function myplugin_deactivate()
{
  // Do something
}
?>

NOTE: It’s important to remember that deactivating is not uninstalling. You should never include uninstall functionality in your deactivation function. Imagine that a user accidentally deactivates your plugin and all of their settings are deleted.

Uninstalling Your Plugin

WordPress features two ways to register the uninstaller for your plugin: the uninstall.php file method and the uninstall hook. Both methods are executed when a deactivated plugin is deleted in the WordPress admin dashboard. The first step to using the first method is to create an uninstall.php file. This file must exist in the root directory of your plugin, and if it does, it will execute in preference to the uninstall hook.

<?php
// If uninstall/delete not called from WordPress then exit
if(!defined( 'ABSPATH' ) && !defined('WP_UNINSTALL_PLUGIN'))
    exit();

// Delete option from options table
delete_option( 'myplugin_options_arr' );

// Delete any other options, custom tables/data, files
?>

The first thing your uninstall.php file should check is that ABSPATH and WP_UNINSTALL_PLUGIN constants have been defined, meaning they were actually called from WordPress. This is a security measure to ensure this file is not executed except during the uninstall process of your plugin. The next step is to remove any options and custom tables your plugin created. In a perfect uninstall scenario there would be no trace of your plugin left over in the database once it had been uninstalled. The preceding example uses delete_option() to delete the option array. Remember that once this function runs, all custom plugin data saved will be destroyed.

The second method for uninstalling a plugin is to use the Uninstall hook. When a plugin is deleted, and uninstall.php does not exist but the Uninstall hook does exist, the plugin will be run one last time to execute the Uninstall hook. After the hook has been called, your plugin will be deleted.
Let’s take a look at the Uninstall hook in action:

<?php
register_uninstall_hook( __FILE__, 'myplugin_uninstall_hook');

function myplugin_uninstall_hook() 
{
    delete_option('myplugin_options_arr');
    //remove any additional options and custom tables
}
?>

Now you are ready for the fun part: start building your plugin features.

Share            
X
                                                                                                        

Leave a Reply

Your email address will not be published. Required fields are marked *