Using WordPress ‘create_term’ PHP action

The create_term WordPress PHP action fires immediately after a new term is created, before the term cache is cleaned. The create_$taxonomy hook is also available for targeting a specific taxonomy.

Usage

add_action('create_term', 'my_custom_function', 10, 4);

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

    return $term_id;
}

Parameters

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

More information

See WordPress Developer Resources: create_term

Examples

Update term meta after term creation

This example updates the term meta after a term has been created.

add_action('create_term', 'update_term_meta_on_creation', 10, 4);

function update_term_meta_on_creation($term_id, $tt_id, $taxonomy, $args) {
    update_term_meta($term_id, 'term_meta_key', 'term_meta_value');
}

Send a notification when a term is created

This example sends an email notification to the site administrator when a new term is created.

add_action('create_term', 'send_email_on_new_term', 10, 4);

function send_email_on_new_term($term_id, $tt_id, $taxonomy, $args) {
    $admin_email = get_option('admin_email');
    $term = get_term($term_id);
    $subject = "New Term Created: {$term->name}";
    $message = "A new term has been created in the {$taxonomy} taxonomy.";

    wp_mail($admin_email, $subject, $message);
}

Log term creation

This example logs term creation to a custom log file.

add_action('create_term', 'log_term_creation', 10, 4);

function log_term_creation($term_id, $tt_id, $taxonomy, $args) {
    $term = get_term($term_id);
    $log_message = "Term {$term->name} (ID: {$term_id}) created in {$taxonomy} taxonomy.";
    error_log($log_message, 3, '/path/to/your/logfile.log');
}

Assign a default parent term

This example assigns a default parent term to a newly created term if no parent is set.

add_action('create_term', 'assign_default_parent_term', 10, 4);

function assign_default_parent_term($term_id, $tt_id, $taxonomy, $args) {
    if ($taxonomy == 'your_custom_taxonomy' && $args['parent'] == 0) {
        $default_parent_term_id = 123;
        wp_update_term($term_id, $taxonomy, ['parent' => $default_parent_term_id]);
    }
}

Trigger a custom function when a specific taxonomy term is created

This example triggers a custom function when a term is created in a specific taxonomy.

add_action('create_term', 'custom_function_for_specific_taxonomy', 10, 4);

function custom_function_for_specific_taxonomy($term_id, $tt_id, $taxonomy, $args) {
    if ($taxonomy == 'your_custom_taxonomy') {
        // your custom code here
    }
}