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.
Alright folks, let’s talk about the activated_plugin action hook in WordPress. If you’re an Indian developer like me, you’ll find this guide super handy. This hook doesn’t fire when a plugin is silently activated, like during an update. To use the activated_plugin action, you’ll need to register it using add_action. You can drop this code into your theme’s functions.php file or create a custom WordPress Plugin. At WePlugins, we always go for custom plugins to avoid breaking anything when you update your theme.
Here’s the deal: we’ll define a function execute_on_activated_plugin_event which takes two parameters and register it with add_action. The parameters are the hook name activated_plugin, the function name execute_on_activated_plugin_event, the priority, and the number of arguments. Sometimes, you may need to deregister a hook, and for that, you can use remove_action to remove the activated_plugin action.
Below are the two parameters required to use this hook:
This example shows how you can use the activated_plugin hook in your code.
function weplugins_detect_plugin_activation($plugin, $network_activation) {
// do stuff
}
add_action('activated_plugin', 'weplugins_detect_plugin_activation', 10, 2);
Here’s how to execute a custom function when a plugin is activated.
function weplugins_execute_on_activated_plugin_event($plugin, $network_wide){
//You can write code here to be executed when this action occurs in WordPress.
}
// add the action
add_action( "activated_plugin", "weplugins_execute_on_activated_plugin_event" , 10, 2);
To remove a hook callback, use the example below.
remove_action( "activated_plugin", "weplugins_execute_on_activated_plugin_event", 10, 2 );
Make sure to provide the same callback function name, priority, and number of arguments when removing the hook callback.
If you need any customization or run into any issues using this hook, feel free to Contact Us. We’re here to help!
Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.