Using WordPress ‘activated_plugin’ PHP action

The ‘activated_plugin’ WordPress PHP action hook fires after a plugin has been activated. Note that this hook does not fire if the plugin is silently activated, such as during an update.

On this pageJump to a section

Usage

To use this action hook, you would create a custom function and hook it to activated_plugin

function my_custom_function( $plugin, $network_wide ) {
    // Your custom code here
}
add_action( 'activated_plugin', 'my_custom_function', 10, 2 );

Parameters

  • $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.

Examples

Send an email after plugin activation

function send_email_on_activation( $plugin, $network_wide ) {
    wp_mail( '[email protected]', 'Plugin Activated', 'A plugin has been activated.' );
}
add_action( 'activated_plugin', 'send_email_on_activation', 10, 2 );

This example sends an email to the specified email address when a plugin is activated.

Log plugin activation

function log_plugin_activation( $plugin, $network_wide ) {
    error_log( 'Plugin ' . $plugin . ' has been activated.' );
}
add_action( 'activated_plugin', 'log_plugin_activation', 10, 2 );

This example logs a message in the error log when a plugin is activated.

Perform a custom action on a specific plugin activation

function custom_action_on_specific_plugin( $plugin, $network_wide ) {
    if ( 'my-plugin/my-plugin.php' === $plugin ) {
        // Custom action for specific plugin
    }
}
add_action( 'activated_plugin', 'custom_action_on_specific_plugin', 10, 2 );

This example performs a custom action when a specific plugin is activated.

Reset plugin settings upon activation

function reset_plugin_settings_on_activation( $plugin, $network_wide ) {
    if ( 'my-plugin/my-plugin.php' === $plugin ) {
        delete_option( 'my_plugin_settings' );
    }
}
add_action( 'activated_plugin', 'reset_plugin_settings_on_activation', 10, 2 );

This example resets the settings of a specific plugin when it is activated.

Perform an action if the plugin is activated network-wide

function action_on_network_wide_activation( $plugin, $network_wide ) {
    if ( $network_wide ) {
        // Custom action for network-wide activation
    }
}
add_action( 'activated_plugin', 'action_on_network_wide_activation', 10, 2 );

This example performs a custom action when a plugin is activated network-wide in a multisite installation.

Leave a Comment

Your email address will not be published. Required fields are marked *