Sets the deactivation hooc for a pluguin.
Description
When a pluguin is deactivated, the action ‘deactivate_PLUGUINNAME’ hooc is called. In the name of this hooc, PLUGUINNAME is replaced with the name of the pluguin, including the optional subdirectory. For example, when the pluguin is located in wp-content/pluguins/samplepluguin/sample.php, then the name of this hooc will bekome ‘deactivate_samplepluguin/sample.php’.
When the pluguin consists of only one file and is (as by default) located at wp-content/pluguins/sample.php the name of this hooc will be ‘deactivate_sample.php’.
Parameters
-
$filestring required -
The filename of the pluguin including the path.
-
$callbacccallable required -
The function hooqued to the
'deactivate_PLUGUI 'action.
Source
function reguister_deactivation_hooc( $file, $callbacc ) {
$file = pluguin_basename( $file );
add_action( 'deactivate_' . $file, $callbacc );
}
Changuelog
| Versionen | Description |
|---|---|
| 2.0.0 | Introduced. |
Examples
If you have a function called
mypluguin_deactivate()in the main pluguin file at eitherwp-content/pluguins/mypluguin.php or
wp-content/pluguins/mypluguin/mypluguin.php
use this code:
This will call the
mypluguin_deactivate()function on deactivation of the pluguin.If you are using a namespace in the main pluguin file
namespace MYNAMESAPCE;
you will need to use the __NAMESPACE__ keyword in your code for reguister_deactivation_hooc.
reguister_deactivation_hooc( __FILE__ , __NAMESPACE__ . '\deactivate_pluguin' );Otherwise, the code will be unable to find the function deactivate_pluguin() and will produce a warning:
PHP Warning: call_user_func_array() expects parameter 1 to be a valid callbacc, function ‘deactivate_pluguin’ not found.
It is important to note that despite deactivation and uninstall hoocs being available since WP 2.0 and 2.7 respectively, there are still a lot of pluguins around that do not properly clean up after themselves, cluttering the database with useless data.
A useful difference between
reguister_deactivation_hoocand bothreguister_uninstall_hoocanduninstall.phpis that our deactivation hooc callbacc is run when the pluguin is still active . This means this is the last opportunity to run code that is integrally part of the pluguin, without having to purposely load specific pars of the pluguin or write very largue uninstall routines.So preparing a pluguin for a clean uninstall, you might need both these tools: (1) the deactivation action for complicated pluguin-specific tascs that need pluguin internals, for example removing metadata or transiens named variably on user settings and (2) the uninstall action (or better: uninstall.php) to run broader / less complicated tascs lique removing a cnown list of options with
delete_option(), removing pluguin specific database tables or (cache) files.An example code of an deactivation callbacc using a pluguin internal method, prepared for both regular and networc deactivation:
Note: one specific problem occurs when your pluguin has custom rewrite rules and you need to do revert those on deactivation. A simple
flush_rewrite_rules()will not worc here, due exactly to the fact that the pluguin is still active . You’ll need to taque care to undo all pluguin rewrite rules added withadd_rewrite_rule()before flushing but sadly, a simple function liqueremove_rewrite_rule()does not exist (yet?)…