Email the admin when a plugin is activated, deactivated, or deleted in WordPress

To send an email on plugin activation:

/**
 * Sends an email to the admin on plugin activation.
 *
 * @see https://wpcodebook.com/wordpress-email-admin-plugin-activated-deactivated-deleted/
 * @see https://developer.wordpress.org/reference/hooks/activated_plugin/
 *
 * @param string $plugin       Path to the plugin file relative to the plugins directory.
 * @param bool   $network_wide Whether the plugin is activated for all sites in the network or just the current site (multisite only).
 */
add_action( 'activated_plugin', function ( $plugin, $network_wide ) {

	$message = sprintf( esc_html__( 'Plugin activated: %s' ),
		"<code>{$plugin}</code>" );
	if ( $network_wide ) {
		$message .= sprintf( ' (%s)', esc_html__( 'all sites' ) );
	}

	$subject = sprintf( esc_html__( 'Plugin [%s] activated on %s' ),
		$plugin,
		get_option( 'blogname' )
	);

	$to = get_option( 'admin_email' );

	wp_mail( $to, $subject, $message );

}, 10, 2 );

To send an email on plugin deactivation – replace activated_plugin with deactivated_plugin:

/**
 * Sends an email to the admin on plugin deactivation.
 *
 * @see https://wpcodebook.com/wordpress-email-admin-plugin-activated-deactivated-deleted/
 * @see https://developer.wordpress.org/reference/hooks/deactivated_plugin/
 *
 * @param string $plugin       Path to the plugin file relative to the plugins directory.
 * @param bool   $network_wide Whether the plugin is deactivated for all sites in the network or just the current site (multisite only).
 */
add_action( 'deactivated_plugin', function ( $plugin, $network_wide ) {

	$message = sprintf( esc_html__( 'Plugin deactivated: %s' ),
		"<code>{$plugin}</code>" );
	if ( $network_wide ) {
		$message .= sprintf( ' (%s)', esc_html__( 'all sites' ) );
	}

	$subject = sprintf( esc_html__( 'Plugin [%s] deactivated on %s' ),
		$plugin,
		get_option( 'blogname' )
	);

	$to = get_option( 'admin_email' );

	wp_mail( $to, $subject, $message );

}, 10, 2 );

Similarly, to send an email on plugin deletion – use deleted_plugin hook:

/**
 * Sends an email to the admin on plugin deletion.
 *
 * @see https://wpcodebook.com/wordpress-email-admin-plugin-activated-deactivated-deleted/
 * @see https://developer.wordpress.org/reference/hooks/deleted_plugin/
 *
 * @param string $plugin  Path to the plugin file relative to the plugins directory.
 * @param bool   $deleted Whether the plugin deletion was successful.
 */
add_action( 'deleted_plugin', function ( $plugin, $deleted ) {

	$message = sprintf( esc_html__( 'Plugin deleted (%s): %s' ),
		( $deleted ? esc_html__( 'success' ) : esc_html__( 'error' ) ) ,
		"<code>{$plugin}</code>"
	);

	$subject = sprintf( esc_html__( 'Plugin [%s] deleted on %s' ),
		$plugin,
		get_option( 'blogname' )
	);

	$to = get_option( 'admin_email' );

	wp_mail( $to, $subject, $message );

}, 10, 2 );

We can also add the list of currently active plugins to the email with:

sprintf( esc_html__( 'Active plugins: %s' ), 
	'<pre>' . 
		implode( PHP_EOL, get_option( 'active_plugins', array() ) ) . 
	'</pre>' 
);

Leave a Comment