Using WordPress ‘edited_term’ PHP action

The edited_term WordPress PHP action fires after a term has been updated and the term cache has been cleaned. The edited_$taxonomy hook is also available for targeting a specific taxonomy.

Usage

add_action('edited_term', 'your_custom_function', 10, 4);

function your_custom_function($term_id, $tt_id, $taxonomy, $args) {
  // your custom code here
}

Parameters

  • $term_id (int): Term ID.
  • $tt_id (int): Term taxonomy ID.
  • $taxonomy (string): Taxonomy slug.
  • $args (array): Arguments passed to wp_update_term().

More information

See WordPress Developer Resources: edited_term

Examples

Update the post count for a custom taxonomy

add_action('edited_term', 'update_custom_taxonomy_post_count', 10, 4);

function update_custom_taxonomy_post_count($term_id, $tt_id, $taxonomy, $args) {
  if ('your_custom_taxonomy' === $taxonomy) {
    // Get the term object
    $term = get_term($term_id, $taxonomy);

    // Update the post count for the custom taxonomy
    update_field('post_count', $term->count, $term);
  }
}

Log term updates for a specific taxonomy

add_action('edited_term', 'log_term_updates', 10, 4);

function log_term_updates($term_id, $tt_id, $taxonomy, $args) {
  if ('your_specific_taxonomy' === $taxonomy) {
    // Log the term updates
    error_log("Term with ID: {$term_id} in taxonomy: {$taxonomy} has been updated.");
  }
}

Send an email notification when a term is edited

add_action('edited_term', 'send_term_edited_email', 10, 4);

function send_term_edited_email($term_id, $tt_id, $taxonomy, $args) {
  // Set email parameters
  $to = '[email protected]';
  $subject = "Term {$term_id} has been edited";
  $message = "Term with ID: {$term_id} in taxonomy: {$taxonomy} has been updated.";

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

Add a custom field to a term when it’s edited

add_action('edited_term', 'add_custom_field_to_term', 10, 4);

function add_custom_field_to_term($term_id, $tt_id, $taxonomy, $args) {
  // Add a custom field to the term
  add_term_meta($term_id, 'custom_field_name', 'custom_field_value', true);
}

Update the term metadata when a term is edited

add_action('edited_term', 'update_term_metadata', 10, 4);

function update_term_metadata($term_id, $tt_id, $taxonomy, $args) {
  // Get the current term metadata
  $current_metadata = get_term_meta($term_id, 'metadata_key', true);

  // Update the term metadata with new value
  $new_metadata_value = 'new_value';
  update_term_meta($term_id, 'metadata_key', $new_metadata_value, $current_metadata);
}