Using WordPress ‘edited_{$taxonomy}’ PHP action

The edited_{$taxonomy} WordPress PHP action fires after a term for a specific taxonomy has been updated, and the term cache has been cleaned.

Usage

add_action('edited_{$taxonomy}', 'my_custom_function', 10, 3);

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

Parameters

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

More information

See WordPress Developer Resources: edited_{$taxonomy}

Examples

Send an email when a category is updated

Send an email to the site administrator when a category is updated.

add_action('edited_category', 'email_on_category_update', 10, 3);

function email_on_category_update($term_id, $tt_id, $args) {
    $term = get_term($term_id);
    $subject = 'Category Updated: ' . $term->name;
    $message = 'The category "' . $term->name . '" has been updated.';
    wp_mail(get_option('admin_email'), $subject, $message);
}

Log post tag updates

Log post tag updates to a custom log file.

add_action('edited_post_tag', 'log_post_tag_updates', 10, 3);

function log_post_tag_updates($term_id, $tt_id, $args) {
    $term = get_term($term_id);
    $log_message = 'Post tag "' . $term->name . '" updated on ' . date('Y-m-d H:i:s') . "\n";
    error_log($log_message, 3, WP_CONTENT_DIR . '/my-custom-log.log');
}

Update custom term meta on term update

Update custom term meta when a term is updated.

add_action('edited_{$taxonomy}', 'update_custom_term_meta', 10, 3);

function update_custom_term_meta($term_id, $tt_id, $args) {
    update_term_meta($term_id, 'custom_meta_key', 'new_meta_value');
}

Update custom field for posts when a category is updated

Update a custom field value for all posts in a category when the category is updated.

add_action('edited_category', 'update_posts_custom_field_on_category_update', 10, 3);

function update_posts_custom_field_on_category_update($term_id, $tt_id, $args) {
    $posts = get_posts(array('category' => $term_id));

    foreach ($posts as $post) {
        update_post_meta($post->ID, 'custom_field_key', 'new_value');
    }
}

Clear custom cache on term update

Clear custom cache when a term is updated.

add_action('edited_{$taxonomy}', 'clear_custom_cache_on_term_update', 10, 3);

function clear_custom_cache_on_term_update($term_id, $tt_id, $args) {
    delete_transient('my_custom_cache_key');
}