Using WordPress ‘delete_site_option’ PHP action

The delete_site_option WordPress PHP action fires after a network option has been deleted.

Usage

add_action('delete_site_option', 'your_custom_function', 10, 2);

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

Parameters

  • $option (string): Name of the network option.
  • $network_id (int): ID of the network.

More information

See WordPress Developer Resources: delete_site_option

Examples

Log deleted network option

Log the deleted network option to a custom log file.

add_action('delete_site_option', 'log_deleted_network_option', 10, 2);

function log_deleted_network_option($option, $network_id) {
    // Log the deleted network option
    error_log("Network Option '{$option}' has been deleted from Network ID: {$network_id}");
}

Send email notification

Send an email notification to the administrator when a specific network option is deleted.

add_action('delete_site_option', 'send_email_on_option_delete', 10, 2);

function send_email_on_option_delete($option, $network_id) {
    if ($option === 'important_option') {
        // Send email to the administrator
        wp_mail('[email protected]', 'Important Option Deleted', 'The important_option has been deleted from Network ID: ' . $network_id);
    }
}

Update another option’s value based on the deletion of a specific network option.

add_action('delete_site_option', 'update_related_option', 10, 2);

function update_related_option($option, $network_id) {
    if ($option === 'theme_color') {
        // Reset the related option
        update_site_option('theme_header_color', 'default');
    }
}

Trigger custom action

Trigger a custom action when a specific network option is deleted.

add_action('delete_site_option', 'trigger_custom_action_on_delete', 10, 2);

function trigger_custom_action_on_delete($option, $network_id) {
    if ($option === 'custom_plugin_settings') {
        // Trigger custom action
        do_action('custom_plugin_settings_deleted', $network_id);
    }
}

Delete related data from the database when a network option is deleted.

add_action('delete_site_option', 'clean_up_related_data', 10, 2);

function clean_up_related_data($option, $network_id) {
    if ($option === 'plugin_data') {
        // Delete related data
        global $wpdb;
        $wpdb->delete($wpdb->prefix . 'plugin_related_data', array('network_id' => $network_id));
    }
}