Using WordPress ‘deleted_term_taxonomy’ PHP action

The deleted_term_taxonomy WordPress action fires immediately after a term taxonomy ID is deleted.

Usage

add_action('deleted_term_taxonomy', 'your_custom_function', 10, 1);

function your_custom_function($tt_id) {
  // your custom code here
}

Parameters

  • $tt_id (int) – The term taxonomy ID that was deleted.

More information

See WordPress Developer Resources: deleted_term_taxonomy

Examples

Logging term taxonomy deletion

Log term taxonomy deletions in a custom log file.

add_action('deleted_term_taxonomy', 'log_term_taxonomy_deletion', 10, 1);

function log_term_taxonomy_deletion($tt_id) {
  // Log term taxonomy deletion
  error_log("Term taxonomy ID {$tt_id} was deleted.");
}

Notify admin on term taxonomy deletion

Send an email notification to the admin when a term taxonomy is deleted.

add_action('deleted_term_taxonomy', 'notify_admin_term_taxonomy_deletion', 10, 1);

function notify_admin_term_taxonomy_deletion($tt_id) {
  // Prepare email
  $to = get_option('admin_email');
  $subject = 'Term Taxonomy Deleted';
  $message = "Term taxonomy ID {$tt_id} was deleted.";

  // Send email
  wp_mail($to, $subject, $message);
}

Update custom counter on term taxonomy deletion

Update a custom counter when a term taxonomy is deleted.

add_action('deleted_term_taxonomy', 'update_custom_counter', 10, 1);

function update_custom_counter($tt_id) {
  // Update custom counter
  $counter = get_option('custom_term_taxonomy_counter', 0);
  $counter--;
  update_option('custom_term_taxonomy_counter', $counter);
}

Remove term taxonomy meta on deletion

Remove term taxonomy meta data when a term taxonomy is deleted.

add_action('deleted_term_taxonomy', 'remove_term_taxonomy_meta', 10, 1);

function remove_term_taxonomy_meta($tt_id) {
  // Remove term taxonomy meta data
  delete_metadata('term', $tt_id, '_custom_meta_key');
}

Sync term taxonomy deletion with external system

Synchronize term taxonomy deletion with an external system.

add_action('deleted_term_taxonomy', 'sync_term_taxonomy_deletion', 10, 1);

function sync_term_taxonomy_deletion($tt_id) {
  // Synchronize with external system
  $response = wp_remote_post('https://external-system.com/api/delete_term_taxonomy', array(
    'body' => array('tt_id' => $tt_id)
  ));
}