Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use deactivated_plugin action in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
May 19, 2023
5 minutes read

Ever faced a situation where you need to know when a plugin gets deactivated in WordPress? That’s where the deactivated_plugin action hook comes into play. It’s super useful for developers who want to perform specific tasks when a plugin is deactivated. But hey, remember, if a plugin is silently deactivated during an update, this hook won’t fire. So, make sure you register it properly.

To get started with the deactivated_plugin action, you need to register it using add_action. You can do this in your theme’s functions.php or, even better, in a custom WordPress Plugin. At WePlugins, we always suggest creating a custom plugin for hooks to keep your theme updates smooth and hassle-free.

Example 1: Basic Hook Usage

Here’s a straightforward example to get you started with the deactivated_plugin action hook.

    function weplugins_execute_on_deactivated_plugin_event($plugin, $network_deactivating){
        // Code to execute when this action occurs in WordPress
    }
    // Add the action
    add_action("deactivated_plugin", "weplugins_execute_on_deactivated_plugin_event", 10, 2);
    

Example 2: Removing the Hook

If you ever need to remove a registered hook, use remove_action like this.

    remove_action("deactivated_plugin", "weplugins_execute_on_deactivated_plugin_event", 10, 2);
    

Example 3: Using Parameters

Below is how you can use the parameters with this hook.

    function weplugins_execute_on_deactivated_plugin_event($plugin, $network_deactivating){
        // Implement custom functionality using $plugin and $network_deactivating
    }
    add_action("deactivated_plugin", "weplugins_execute_on_deactivated_plugin_event", 10, 2);
    

Access Premium WordPress Plugins

If you need any customization or run into issues, feel free to Contact Us for expert assistance.

Sandeep Kumar Mishra

Sandeep Kumar Mishra

Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.

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.