Using WordPress ‘deleted_theme’ PHP action

The deleted_theme WordPress PHP action fires immediately after a theme deletion attempt.

Usage

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

function your_custom_function($stylesheet, $deleted) {
  // your custom code here
}

Parameters

  • $stylesheet (string) – Stylesheet of the theme to delete.
  • $deleted (bool) – Whether the theme deletion was successful.

More information

See WordPress Developer Resources: deleted_theme

Examples

Log theme deletion

Log theme deletion attempts to a custom log file.

add_action('deleted_theme', 'log_theme_deletion', 10, 2);

function log_theme_deletion($stylesheet, $deleted) {
  $log_message = $deleted ? "Theme deletion successful: " : "Theme deletion failed: ";
  $log_message .= $stylesheet;
  error_log($log_message, 3, "/path/to/your/logfile.log");
}

Send email notification

Send an email notification when a theme is deleted.

add_action('deleted_theme', 'send_theme_deletion_email', 10, 2);

function send_theme_deletion_email($stylesheet, $deleted) {
  if ($deleted) {
    $to = '[email protected]';
    $subject = 'Theme Deleted';
    $message = 'The theme ' . $stylesheet . ' has been deleted.';
    wp_mail($to, $subject, $message);
  }
}

Update theme deletion counter

Update a custom counter for theme deletions.

add_action('deleted_theme', 'update_theme_deletion_counter', 10, 2);

function update_theme_deletion_counter($stylesheet, $deleted) {
  if ($deleted) {
    $counter = (int) get_option('theme_deletion_counter', 0);
    $counter++;
    update_option('theme_deletion_counter', $counter);
  }
}

Display an admin notice

Display a custom admin notice after a theme is deleted.

add_action('deleted_theme', 'show_theme_deletion_notice', 10, 2);

function show_theme_deletion_notice($stylesheet, $deleted) {
  $notice = $deleted ? "Theme deletion successful: " : "Theme deletion failed: ";
  $notice .= $stylesheet;
  set_transient('theme_deletion_notice', $notice, 60);
}

add_action('admin_notices', 'display_theme_deletion_notice');

function display_theme_deletion_notice() {
  $notice = get_transient('theme_deletion_notice');
  if ($notice) {
    echo '<div class="notice notice-success is-dismissible"><p>' . $notice . '</p></div>';
    delete_transient('theme_deletion_notice');
  }
}

Perform cleanup tasks

Perform cleanup tasks after a theme is deleted.

add_action('deleted_theme', 'perform_cleanup_tasks', 10, 2);

function perform_cleanup_tasks($stylesheet, $deleted) {
  if ($deleted) {
    // Clean up theme-specific options, transients, etc.
    delete_option('your_theme_option');
    delete_transient('your_theme_transient');
  }
}