This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.
deactivate_plugin action
This hook is the “deactivation” hook used internally by register_deactivation_hook()
. The dynamic portion of the hook name, $plugin
, refers to the plugin basename.
To use the deactivate_plugin
action, you first need to register it using add_action
. You can write this code into functions.php
of your activated theme or in a custom WordPress Plugin.
WePlugins always prefer creating a custom WordPress Plugin while using hooks so nothing breaks when you update your WordPress Theme in the future.
In the below live example, we have defined a function weplugins_execute_on_deactivate_plugin_event
which takes 1 parameter, and we registered it using add_action
. The first parameter deactivate_plugin
is the name of the hook. The second parameter weplugins_execute_on_deactivate_plugin_event
is the name of the function which needs to be called. The third parameter is the priority of calling the hook if the same hook is used multiple times, and the last parameter is the number of arguments (if any) to be passed to the registered function.
Sometimes, you have to remove a registered hook, so you can use remove_action
to remove the deactivate_plugin
action.
Parameters
- $network_deactivating: (bool) Whether the plugin is deactivated for all sites in the network or just the current site. Multisite only. Default false.
Below is the 1 parameter required to use this hook.
Live Example
Below is an example of how you can use this hook.
function weplugins_execute_on_deactivate_plugin_event($network_deactivating){ // You can write code here to be executed when this action occurs in WordPress. Use the parameters received in the function arguments & implement the required additional custom functionality according to your website requirements. } // add the action add_action( "deactivate_plugin", "weplugins_execute_on_deactivate_plugin_event" , 10, 1);
Example 1: Simple Deactivation Action
This example demonstrates a simple deactivation action where we log the deactivation event.
function weplugins_log_deactivation_event($network_deactivating){ error_log("Plugin deactivated. Network-wide: " . ($network_deactivating ? 'Yes' : 'No')); } add_action( "deactivate_plugin", "weplugins_log_deactivation_event" , 10, 1);
Example 2: Cleanup on Deactivation
In this example, we clean up custom database entries when the plugin is deactivated.
function weplugins_cleanup_on_deactivate($network_deactivating){ global $wpdb; $wpdb->query("DELETE FROM {$wpdb->prefix}custom_table WHERE plugin_active = 0"); } add_action( "deactivate_plugin", "weplugins_cleanup_on_deactivate" , 10, 1);
Example 3: Notify Admin on Deactivation
This example sends an email notification to the site admin when the plugin is deactivated.
function weplugins_notify_admin_on_deactivate($network_deactivating){ $admin_email = get_option('admin_email'); wp_mail($admin_email, 'Plugin Deactivated', 'A plugin has been deactivated on your site.'); } add_action( "deactivate_plugin", "weplugins_notify_admin_on_deactivate" , 10, 1);
To remove a hook callback, use the example below.
remove_action( "deactivate_plugin", "weplugins_execute_on_deactivate_plugin_event", 10, 1 );
Please make sure to provide the same callback function name, priority, and number of arguments while removing the hook callback.
Contact Us
If you’re having any trouble using this hook or need customization, please contact us.
Explore the latest in WordPress
Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.