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 activated, 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 'activate_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 'activate_sample.php'.

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

register_deactivation_hook() 描述


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

register_deactivation_hook() 用法



(string) (必填) Path to the main plugin file inside the wp-content/plugins directory. A full path will work.

默认值: None


(callback) (必填) The function to be run when the plugin is deactivated. Any of PHP's callback pseudo-types will work.

默认值: None

register_deactivation_hook() 参数


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:

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

register_deactivation_hook() 示例