WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/register deactivation hook

Description

The function register_deactivation_hook (introduced in WordPress 2.0) registers a plugin function to be run when the plugin is deactivated.

When a plugin is deactivated, the action 'deactivate_PLUGINNAME' hook is called. In the name of this hook, PLUGINNAME is replaced with the name of the plugin, including the optional subdirectory. For example, when the plugin is located in wp-content/plugin/sampleplugin/sample.php, then the name of this hook will become 'deactivate_sampleplugin/sample.php'. When the plugin consists of only one file and is (as by default) located at wp-content/plugin/sample.php the name of this hook will be 'deactivate_sample.php'.

This function is a wrapper for the 'deactivate_PLUGINNAME' action, and is easier to use.

Usage

 <?php register_deactivation_hook($file$function); ?> 

Parameters

$file
(string) (required) Path to the main plugin file inside the wp-content/plugins directory. A full path will work.
Default: None
$function
(callback) (required) The function to be run when the plugin is deactivated. Any of PHP's callback pseudo-types will work.
Default: None

Examples

If you have a function called myplugin_deactivate() in the main plugin file at either

  • wp-content/plugins/myplugin.php or
  • wp-content/plugins/myplugin/myplugin.php

use this code:

register_deactivation_hook( __FILE__, 'myplugin_deactivate' );

This will call the myplugin_deactivate() function on deactivation of the plugin.

Discussions

A good example for a basic activation/deactivation/uninstall class by "kaiser" can be found here on WPSE: http://wordpress.stackexchange.com/questions/25910/uninstall-a-plugin-method-typical-features-how-to/25979#25979

Related

See also index of Function Reference and index of Template Tags.