Using WordPress ‘delete_term_taxonomy’ PHP action

The delete_term_taxonomy WordPress action is used to execute custom code just before a term taxonomy ID is deleted.

Usage

add_action('delete_term_taxonomy', 'your_custom_function');
function your_custom_function($tt_id) {
  // your custom code here
}

Parameters

  • $tt_id (int): The term taxonomy ID that is going to be deleted.

More information

See WordPress Developer Resources: delete_term_taxonomy

Examples

Log taxonomy deletion

Log the deletion of term taxonomy IDs to a custom file.

add_action('delete_term_taxonomy', 'log_taxonomy_deletion');
function log_taxonomy_deletion($tt_id) {
  $log_message = "Term taxonomy ID {$tt_id} was deleted.";
  error_log($log_message, 3, '/path/to/your/log-file.log');
}

Send email notification

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

add_action('delete_term_taxonomy', 'send_email_on_taxonomy_deletion');
function send_email_on_taxonomy_deletion($tt_id) {
  $admin_email = get_option('admin_email');
  $subject = "Term Taxonomy Deleted";
  $message = "Term taxonomy ID {$tt_id} was deleted.";
  wp_mail($admin_email, $subject, $message);
}

Remove associated metadata

Remove all associated metadata when a term taxonomy ID is deleted.

add_action('delete_term_taxonomy', 'remove_associated_metadata');
function remove_associated_metadata($tt_id) {
  delete_metadata('term', $tt_id);
}

Update counter

Update a custom counter when a term taxonomy ID is deleted.

add_action('delete_term_taxonomy', 'update_custom_counter');
function update_custom_counter($tt_id) {
  $counter = get_option('my_taxonomy_delete_counter');
  $counter++;
  update_option('my_taxonomy_delete_counter', $counter);
}

Add a custom log entry

Add a custom log entry in the database when a term taxonomy ID is deleted.

add_action('delete_term_taxonomy', 'add_custom_log_entry');
function add_custom_log_entry($tt_id) {
  global $wpdb;
  $table_name = $wpdb->prefix . "custom_log";
  $wpdb->insert($table_name, array('taxonomy_id' => $tt_id, 'action' => 'deleted'));
}