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.
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);
If you need any customization or run into issues, feel free to Contact Us for expert assistance.
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.