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.
Opens WPIDE on
Code
// 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 add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.