Using WordPress ‘edit_term_taxonomy’ PHP action

The edit_term_taxonomy WordPress PHP action fires immediately before a term-taxonomy relationship is updated.

Usage

add_action('edit_term_taxonomy', 'my_custom_function', 10, 3);

function my_custom_function($tt_id, $taxonomy, $args) {
    // Your custom code here

    return $tt_id;
}

Parameters

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

More information

See WordPress Developer Resources: edit_term_taxonomy

Examples

Log term updates

Log every term update in a custom log file.

add_action('edit_term_taxonomy', 'log_term_updates', 10, 3);

function log_term_updates($tt_id, $taxonomy, $args) {
    $log_message = "Term taxonomy ID: {$tt_id}, Taxonomy: {$taxonomy}, Arguments: " . json_encode($args);
    error_log($log_message, 3, "/var/log/term_updates.log");
}

Prevent updating specific taxonomy

Prevent updating terms in a custom taxonomy called ‘restricted_taxonomy’.

add_action('edit_term_taxonomy', 'prevent_updating_restricted_taxonomy', 10, 3);

function prevent_updating_restricted_taxonomy($tt_id, $taxonomy, $args) {
    if ($taxonomy == 'restricted_taxonomy') {
        wp_die('You cannot update this taxonomy.');
    }
}

Add a prefix to term slug

Automatically add a prefix to the term slug of a specific taxonomy.

add_action('edit_term_taxonomy', 'add_prefix_to_term_slug', 10, 3);

function add_prefix_to_term_slug($tt_id, $taxonomy, $args) {
    if ($taxonomy == 'my_taxonomy') {
        $args['slug'] = 'my_prefix_' . $args['slug'];
        wp_update_term($tt_id, $taxonomy, $args);
    }
}

Update term description

Update the term description with a custom string.

add_action('edit_term_taxonomy', 'update_term_description', 10, 3);

function update_term_description($tt_id, $taxonomy, $args) {
    $args['description'] = 'Custom description for term ID: ' . $tt_id;
    wp_update_term($tt_id, $taxonomy, $args);
}

Make term a child of another term

Update a term to make it a child of another term in the same taxonomy.

add_action('edit_term_taxonomy', 'make_term_child', 10, 3);

function make_term_child($tt_id, $taxonomy, $args) {
    $parent_term_id = 5;
    $args['parent'] = $parent_term_id;
    wp_update_term($tt_id, $taxonomy, $args);
}