Using WordPress ‘deleted_option’ PHP action

The deleted_option WordPress PHP action fires after an option has been deleted.

Usage

add_action('deleted_option', 'my_custom_function', 10, 1);

function my_custom_function($option) {
  // your custom code here
}

Parameters

  • $option (string) – Name of the deleted option.

More information

See WordPress Developer Resources: deleted_option

Examples

Log deleted options

Log the names of deleted options to a file.

add_action('deleted_option', 'log_deleted_option', 10, 1);

function log_deleted_option($option) {
  $log_file = 'deleted_options.log';
  file_put_contents($log_file, $option . PHP_EOL, FILE_APPEND);
}

Send an email notification

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

add_action('deleted_option', 'notify_option_deletion', 10, 1);

function notify_option_deletion($option) {
  if ($option == 'my_important_option') {
    $to = get_option('admin_email');
    $subject = 'Important option deleted';
    $message = 'The option "my_important_option" has been deleted.';
    wp_mail($to, $subject, $message);
  }
}

Update a related option when a specific option is deleted.

add_action('deleted_option', 'update_related_option', 10, 1);

function update_related_option($option) {
  if ($option == 'my_option') {
    update_option('my_related_option', 'default_value');
  }
}

Trigger an action

Trigger a custom action when a specific option is deleted.

add_action('deleted_option', 'my_custom_trigger', 10, 1);

function my_custom_trigger($option) {
  if ($option == 'my_special_option') {
    do_action('my_custom_action');
  }
}

Add a custom notice

Display a custom notice in the WordPress dashboard when an option is deleted.

add_action('deleted_option', 'show_custom_notice', 10, 1);

function show_custom_notice($option) {
  if ($option == 'my_option') {
    add_action('admin_notices', 'display_notice');
  }
}

function display_notice() {
  echo '<div class="notice notice-info is-dismissible"><p><strong>Notice:</strong> The option "my_option" has been deleted.</p></div>';
}