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

How to use activated_plugin action in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
January 12, 2023
5 minutes read

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.

Access Premium WordPress Plugins

    Below are the two parameters required to use this hook:

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

Live Example 1: Basic Hook Usage

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);
  

Live Example 2: Custom Function Execution

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);
  

Live Example 3: Removing the Hook

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.

Contact Us

If you need any customization or run into any issues using this hook, feel free to Contact Us. We’re here to help!

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.