register_uninstall_hook

Set the uninstallation hook for a plugin.

Description

register_uninstall_hook( (string) $file, (callable) $callback ); 

Registers the uninstall hook that will be called when the user clicks on the uninstall link that calls for the plugin to uninstall itself. The link won't be active unless the plugin hooks into the action.

The plugin should not run arbitrary code outside of functions, when registering the uninstall hook. In order to run using the hook, the plugin will have to be included, which means that any code laying outside of a function will be run during the uninstall process. The plugin should not hinder the uninstall process.

If the plugin can not be written without running code within the plugin, then the plugin should create a file named uninstall.php in the base plugin folder. This file will be called, if it exists, during the uninstall process bypassing the uninstall hook. The plugin, when using the uninstall.php should always check for the WP_UNINSTALL_PLUGIN constant, before executing.

Parameters (2)

0. $file (string)
Plugin file.
1. $callback (callable)
The callback to run when the hook is called. Must be a static method or function.

Usage

  1. if ( !function_exists( 'register_uninstall_hook' ) ) { 
  2. require_once ABSPATH . WPINC . '/plugin.php'; 
  3.  
  4. // Plugin file. 
  5. $file = ''; 
  6.  
  7. // The callback to run when the hook is called. Must be 
  8. // a static method or function. 
  9. $callback = null; 
  10.  
  11. // NOTICE! Understand what this does before running. 
  12. $result = register_uninstall_hook($file, $callback); 
  13.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/plugin.php  
  1. function register_uninstall_hook( $file, $callback ) { 
  2. if ( is_array( $callback ) && is_object( $callback[0] ) ) { 
  3. _doing_it_wrong( __FUNCTION__, __( 'Only a static class method or function can be used in an uninstall hook.' ), '3.1.0' ); 
  4. return; 
  5.  
  6. /** 
  7. * The option should not be autoloaded, because it is not needed in most 
  8. * cases. Emphasis should be put on using the 'uninstall.php' way of 
  9. * uninstalling the plugin. 
  10. */ 
  11. $uninstallable_plugins = (array) get_option('uninstall_plugins'); 
  12. $uninstallable_plugins[plugin_basename($file)] = $callback; 
  13.  
  14. update_option('uninstall_plugins', $uninstallable_plugins);