The make_delete_blog WordPress action is triggered when the ‘deleted’ status is added to a site.
Usage
add_action('make_delete_blog', 'my_custom_function');
function my_custom_function($site_id) {
// your custom code here
}
Parameters
$site_id(int) – The site ID.
More information
See WordPress Developer Resources: make_delete_blog
Examples
Send an email notification when a site is deleted
Notify the administrator via email when a site is deleted.
add_action('make_delete_blog', 'email_admin_on_site_deletion');
function email_admin_on_site_deletion($site_id) {
$admin_email = get_option('admin_email');
$subject = 'Site Deleted';
$message = 'A site with the ID ' . $site_id . ' has been deleted.';
wp_mail($admin_email, $subject, $message);
}
Log site deletion in a custom file
Log the deletion of a site in a custom log file.
add_action('make_delete_blog', 'log_site_deletion');
function log_site_deletion($site_id) {
$log_file = WP_CONTENT_DIR . '/site-deletions.log';
$log_entry = date('Y-m-d H:i:s') . ' - Site ID ' . $site_id . ' deleted.' . PHP_EOL;
file_put_contents($log_file, $log_entry, FILE_APPEND);
}
Remove custom user roles when a site is deleted
Delete custom user roles associated with a deleted site.
add_action('make_delete_blog', 'remove_custom_user_roles');
function remove_custom_user_roles($site_id) {
switch_to_blog($site_id);
remove_role('custom_role');
restore_current_blog();
}
Delete custom site options when a site is deleted
Remove custom site options associated with a deleted site.
add_action('make_delete_blog', 'delete_custom_site_options');
function delete_custom_site_options($site_id) {
switch_to_blog($site_id);
delete_option('custom_option_name');
restore_current_blog();
}
Unschedule a custom cron event when a site is deleted
Unschedule a custom cron event associated with a deleted site.
add_action('make_delete_blog', 'unschedule_custom_cron_event');
function unschedule_custom_cron_event($site_id) {
switch_to_blog($site_id);
wp_clear_scheduled_hook('custom_cron_event');
restore_current_blog();
}