Uninstall Your Plugin
A nice feature to include with your plugin is an uninstall feature. WordPress features two ways to register the uninstaller for your plugin: the uninstall.php method and the uninstall hook. Both methods are executed when a deactivated plugin is deleted in WordPress.
The first method you'll look at is the uninstall.php uninstaller method. This is the preferred method for uninstalling a plugin. The first step to using this 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.
// If uninstall/delete not called from WordPress then exit if( ! defined( 'ABSPATH' ) && ! defined( 'WP_UNINSTALL_PLUGIN' ) ) exit () ;
// Delete option from options table delete_option( 'gmp_options_arr' );
//remove any additional options and custom tables global $wpdb;
$table_name = $wpdb->prefix . "gmp_data";
//build our query to delete our custom table
$sql = "DROP TABLE " . $table_name . ";"; //execute the query deleting the table $wpdb->query($sql);
require_once(ABSPATH .'wp-admin/includes/upgrade.php');
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 has been uninstalled. The preceding example uses delete_option to delete the option array. It also runs a drop SQL query to delete the custom plugin table. Remember that once this function runs, all custom plugin data saved will be destroyed.
The second method for uninstalling a plugin is using 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. Here's the uninstall hook in action:
<?php if ( function_exists('register_uninstall_hook') )
register_uninstall_hook(__FILE__, 'gmp_uninstall_hook');
function gmp_uninstall_hook() {
delete_option('gmp_options_arr');
//remove any additional options and custom tables global $wpdb;
$table_name = $wpdb->prefix . "gmp_data";
//build our query to delete our custom table $sql = "DROP TABLE " . $table_name . ";";
//execute the query deleting the table $wpdb->query($sql);
require_once(ABSPATH .'wp-admin/includes/upgrade.php'); dbDelta($sql);
First you want to verify that the register_uninstall_hook function exists. Because the uninstall hook function was added in WordPress 2.7 it won't exist on older versions of WordPress. Next you call your custom uninstall function to properly uninstall your plugin options. If you do include uninstall functionality in your plugin, such as removing custom tables and options, make sure to warn the users that all plugin data will be deleted if they delete the plugin.
The difference between this method and the register_deactivation_hook is that the register_ uninstall_hook is executed when a deactivated plugin is deleted. The register_deactivation_hook is executed when the plugin is deactivated, which means the user may want to activate the plugin again eventually. You wouldn't want to delete all of the plugin settings if the user is planning on using your plugin again.
Post a comment