Using WordPress ‘pre_delete_site_option_{$option}’ PHP action

The pre_delete_site_option_{$option} WordPress action fires immediately before a specific network option is deleted. The dynamic portion of the hook name, $option, refers to the option name.

Usage

add_action('pre_delete_site_option_{$option}', 'your_custom_function', 10, 2);

function your_custom_function($option, $network_id) {
    // your custom code here
}

Parameters

  • $option (string) – Option name.
  • $network_id (int) – ID of the network.

More information

See WordPress Developer Resources: pre_delete_site_option_{$option}

Examples

Log deletion of a specific site option

This example logs when a specific site option, ‘my_custom_option’, is deleted.

add_action('pre_delete_site_option_my_custom_option', 'log_site_option_deletion', 10, 2);

function log_site_option_deletion($option, $network_id) {
    error_log("Deleting site option '{$option}' for network ID {$network_id}");
}

Prevent deletion of a specific site option

This example prevents the deletion of the site option ‘my_protected_option’.

add_action('pre_delete_site_option_my_protected_option', 'prevent_site_option_deletion', 10, 2);

function prevent_site_option_deletion($option, $network_id) {
    wp_die("You cannot delete the '{$option}' option.");
}

Backup a specific site option before deletion

This example creates a backup of the ‘my_important_option’ site option before it’s deleted.

add_action('pre_delete_site_option_my_important_option', 'backup_site_option', 10, 2);

function backup_site_option($option, $network_id) {
    $option_value = get_site_option($option);
    update_site_option("{$option}_backup", $option_value);
}

Send a notification when a specific site option is deleted

This example sends an email notification to the site administrator when the ‘my_critical_option’ site option is deleted.

add_action('pre_delete_site_option_my_critical_option', 'notify_site_option_deletion', 10, 2);

function notify_site_option_deletion($option, $network_id) {
    $admin_email = get_site_option('admin_email');
    wp_mail($admin_email, "Site Option Deleted", "The '{$option}' site option has been deleted.");
}

Perform an action based on the value of a specific site option before deletion

This example performs a custom action based on the value of the ‘my_status_option’ site option before it’s deleted.

add_action('pre_delete_site_option_my_status_option', 'custom_action_on_site_option_deletion', 10, 2);

function custom_action_on_site_option_deletion($option, $network_id) {
    $option_value = get_site_option($option);

    if ($option_value === 'active') {
        // perform custom action when the option value is 'active'
    }
}