Using WordPress ‘delete_theme’ PHP action

The delete_theme WordPress PHP action fires immediately before a theme deletion attempt.

Usage

add_action('delete_theme', 'my_custom_function', 10, 1);
function my_custom_function($stylesheet) {
    // your custom code here
}

Parameters

  • $stylesheet (string) – Stylesheet of the theme to delete.

More information

See WordPress Developer Resources: delete_theme

Examples

Log theme deletion

Log the deletion of a theme with a timestamp.

add_action('delete_theme', 'log_theme_deletion', 10, 1);
function log_theme_deletion($stylesheet) {
    $log_message = 'Theme ' . $stylesheet . ' deleted on ' . date('Y-m-d H:i:s') . "\n";
    error_log($log_message, 3, 'theme_deletion.log');
}

Send email notification

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

add_action('delete_theme', 'email_notification_theme_deletion', 10, 1);
function email_notification_theme_deletion($stylesheet) {
    $to = get_option('admin_email');
    $subject = 'Theme Deleted: ' . $stylesheet;
    $message = 'The theme ' . $stylesheet . ' has been deleted.';
    wp_mail($to, $subject, $message);
}

Backup theme files

Create a backup of the theme’s files before deletion.

add_action('delete_theme', 'backup_theme_files', 10, 1);
function backup_theme_files($stylesheet) {
    $theme_directory = get_theme_root() . '/' . $stylesheet;
    $backup_directory = WP_CONTENT_DIR . '/backups/themes/' . $stylesheet;
    copy_directory($theme_directory, $backup_directory);
}

Update theme deletion record

Update a custom database table with a record of the deleted theme.

add_action('delete_theme', 'update_theme_deletion_record', 10, 1);
function update_theme_deletion_record($stylesheet) {
    global $wpdb;
    $table_name = $wpdb->prefix . 'theme_deletions';
    $data = array('theme' => $stylesheet, 'deleted_at' => current_time('mysql'));
    $wpdb->insert($table_name, $data);
}

Deactivate plugins dependent on the theme

Deactivate plugins that depend on the deleted theme.

add_action('delete_theme', 'deactivate_plugins_dependent_on_theme', 10, 1);
function deactivate_plugins_dependent_on_theme($stylesheet) {
    $dependent_plugins = array(
        'my-plugin/my-plugin.php'
    );

    if ($stylesheet === 'my-theme') {
        foreach ($dependent_plugins as $plugin) {
            deactivate_plugins($plugin);
        }
    }
}