Using WordPress ‘edited_terms’ PHP action

The edited_terms WordPress PHP action fires immediately after a term is updated in the database, but before its term-taxonomy relationship is updated.

Usage

add_action('edited_terms', 'your_custom_function', 10, 3);

function your_custom_function($term_id, $taxonomy, $args) {
    // Your custom code here

    return $term_id;
}

Parameters

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

More information

See WordPress Developer Resources: edited_terms

Examples

Update term meta after term is edited

Update a custom term meta field after the term is updated in the database.

add_action('edited_terms', 'update_custom_term_meta', 10, 3);

function update_custom_term_meta($term_id, $taxonomy, $args) {
    // Update custom term meta
    update_term_meta($term_id, 'custom_meta_key', 'custom_meta_value');
}

Log term changes

Log term changes to a custom log file when a term is updated.

add_action('edited_terms', 'log_term_changes', 10, 3);

function log_term_changes($term_id, $taxonomy, $args) {
    // Log term changes to a custom log file
    error_log("Term ID {$term_id} in taxonomy {$taxonomy} has been updated.");
}

Send email notification after term update

Send an email notification to the administrator when a term is updated.

add_action('edited_terms', 'send_term_update_email', 10, 3);

function send_term_update_email($term_id, $taxonomy, $args) {
    // Send email notification
    $to = get_bloginfo('admin_email');
    $subject = "Term {$term_id} updated in {$taxonomy}";
    $message = "Term ID {$term_id} in taxonomy {$taxonomy} has been updated.";
    wp_mail($to, $subject, $message);
}

Add prefix to term slug

Add a prefix to the term slug when a term is updated.

add_action('edited_terms', 'prefix_term_slug', 10, 3);

function prefix_term_slug($term_id, $taxonomy, $args) {
    // Add prefix to term slug
    $term = get_term($term_id, $taxonomy);
    $new_slug = 'prefix-' . $term->slug;
    wp_update_term($term_id, $taxonomy, array('slug' => $new_slug));
}

Update post count for a custom taxonomy

Update the post count for a custom taxonomy when a term is updated.

add_action('edited_terms', 'update_custom_taxonomy_post_count', 10, 3);

function update_custom_taxonomy_post_count($term_id, $taxonomy, $args) {
    // Update post count for custom taxonomy
    if ($taxonomy === 'custom_taxonomy') {
        $term = get_term($term_id, $taxonomy);
        $post_count = $term->count;
        update_term_meta($term_id, 'custom_taxonomy_post_count', $post_count);
    }
}