# Email Alert When Plugins Are Activated or Deactivated

> Emails the site administrator whenever a plugin is turned on or off, with who did it and when.

- Type: PHP
- Runs: Run everywhere
- Category: [Security](https://wpide.com/library/category/security/)
- Tags: security, plugins, alerts
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/plugin-change-email-alert/

## Code

```php
// Who receives the alerts.
$wpide_plugin_alert_to = get_option( 'admin_email' );

$wpide_plugin_alert = function ( $plugin, $what ) use ( $wpide_plugin_alert_to ) {
    $data = function_exists( 'get_plugin_data' ) && file_exists( WP_PLUGIN_DIR . '/' . $plugin ) ? get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin, false, false ) : array();
    $name = ! empty( $data['Name'] ) ? $data['Name'] : $plugin;
    $user = wp_get_current_user();
    $site = wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES );
    $by   = $user->exists() ? $user->user_login : ( ( defined( 'WP_CLI' ) && WP_CLI ) ? 'WP-CLI' : 'system' );

    wp_mail(
        $wpide_plugin_alert_to,
        sprintf( '[%s] Plugin %s: %s', $site, $what, $name ),
        sprintf( "The plugin \"%s\" was %s on %s.\n\nBy: %s\nTime: %s", $name, $what, home_url( '/' ), $by, wp_date( 'Y-m-d H:i:s T' ) )
    );
};

add_action( 'activated_plugin', function ( $plugin ) use ( $wpide_plugin_alert ) {
    $wpide_plugin_alert( $plugin, 'activated' );
} );

add_action( 'deactivated_plugin', function ( $plugin ) use ( $wpide_plugin_alert ) {
    $wpide_plugin_alert( $plugin, 'deactivated' );
} );
```

## Notes

Bulk changes send one email per plugin.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Email Alert When Plugins Are Activated or Deactivated" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
