Using WordPress ‘deactivated_plugin’ PHP action

The deactivated_plugin WordPress PHP action fires after a plugin is deactivated. If a plugin is silently deactivated (such as during an update), this hook does not fire.

Usage

add_action('deactivated_plugin', 'my_deactivated_plugin_function', 10, 2);

function my_deactivated_plugin_function($plugin, $network_deactivating) {
    // your custom code here
}

Parameters

  • $plugin (string): Path to the plugin file relative to the plugins directory.
  • $network_deactivating (bool): Whether the plugin is deactivated for all sites in the network or just the current site. Multisite only. Default false.

More information

See WordPress Developer Resources: deactivated_plugin

Examples

Log plugin deactivation

Log the plugin deactivation event to a custom log file.

add_action('deactivated_plugin', 'log_plugin_deactivation', 10, 2);

function log_plugin_deactivation($plugin, $network_deactivating) {
    $log_message = sprintf("Plugin %s deactivated. Network-wide: %s", $plugin, $network_deactivating ? 'Yes' : 'No');
    error_log($log_message, 3, "/path/to/custom/logfile.log");
}

Delete plugin options upon deactivation

Delete specific plugin options when the plugin is deactivated.

add_action('deactivated_plugin', 'delete_plugin_options', 10, 2);

function delete_plugin_options($plugin, $network_deactivating) {
    if ($plugin === 'my-plugin/my-plugin.php') {
        delete_option('my_plugin_option_1');
        delete_option('my_plugin_option_2');
    }
}

Send email notification when a plugin is deactivated

Send an email notification to the site administrator when a plugin is deactivated.

add_action('deactivated_plugin', 'send_plugin_deactivation_email', 10, 2);

function send_plugin_deactivation_email($plugin, $network_deactivating) {
    $to = get_option('admin_email');
    $subject = 'Plugin Deactivated';
    $message = sprintf("The %s plugin has been deactivated.", $plugin);
    wp_mail($to, $subject, $message);
}

Redirect after plugin deactivation

Redirect the user to a custom page after the plugin is deactivated.

add_action('deactivated_plugin', 'redirect_after_plugin_deactivation', 10, 2);

function redirect_after_plugin_deactivation($plugin, $network_deactivating) {
    if ($plugin === 'my-plugin/my-plugin.php') {
        wp_redirect(admin_url('admin.php?page=my-custom-page'));
        exit;
    }
}

Deactivate another plugin when a specific plugin is deactivated

Deactivate another plugin when the specified plugin is deactivated.

add_action('deactivated_plugin', 'deactivate_another_plugin', 10, 2);

function deactivate_another_plugin($plugin, $network_deactivating) {
    if ($plugin === 'first-plugin/first-plugin.php') {
        deactivate_plugins('second-plugin/second-plugin.php');
    }
}