Email Alert When a Network Site Is Deleted
Sends an email with the site's name, address and the person responsible whenever a site is permanently deleted from the network.
Opens WPIDE on
Code
if ( ! is_multisite() ) {
return;
}
$wpide_alert_to = get_site_option( 'admin_email' ); // Recipient.
add_action( 'wp_uninitialize_site', function ( $site ) use ( $wpide_alert_to ) {
if ( ! $wpide_alert_to ) {
return;
}
$name = get_blog_option( $site->blog_id, 'blogname' );
$actor = wp_get_current_user();
wp_mail(
$wpide_alert_to,
sprintf( '[%s] Site deleted: %s', get_network()->site_name, $name ),
sprintf(
"Site: %1\$s\nAddress: %2\$s\nID: %3\$d\nDeleted by: %4\$s\nWhen: %5\$s\n",
$name,
$site->domain . $site->path,
$site->blog_id,
$actor->exists() ? $actor->user_login : 'system / WP-CLI',
wp_date( 'Y-m-d H:i' )
)
);
}, 1 );
Notes
Sent just before the site's tables are removed, so the site name is still available.
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.