Using WordPress ‘created_term’ PHP action

The created_term WordPress PHP action fires after a new term is created and after the term cache has been cleaned. The ‘created_$taxonomy’ hook can be used for targeting a specific taxonomy.

Usage

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

function your_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(). Includes alias_of, description, parent, and slug.

More information

See WordPress Developer Resources: created_term

Examples

Send an email notification after a new term is created

Send an email notification to the site administrator after a new term is created in the ‘category’ taxonomy.

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

function send_email_on_new_term($term_id, $tt_id, $taxonomy, $args) {
    $term = get_term($term_id, $taxonomy);
    $subject = 'New Term Created: ' . $term->name;
    $message = 'A new term was created: ' . $term->name . ' (' . $taxonomy . ')';
    wp_mail(get_bloginfo('admin_email'), $subject, $message);
}

Log new terms in a custom log file

Log new term creation in a custom log file, including term ID, term taxonomy ID, taxonomy slug, and term name.

add_action('created_term', 'log_new_terms', 10, 4);

function log_new_terms($term_id, $tt_id, $taxonomy, $args) {
    $term = get_term($term_id, $taxonomy);
    $log_entry = "Term ID: {$term_id}, Term Taxonomy ID: {$tt_id}, Taxonomy: {$taxonomy}, Term Name: {$term->name}\n";
    error_log($log_entry, 3, WP_CONTENT_DIR . '/new_terms.log');
}

Assign a default parent term to new terms

Automatically assign a default parent term for new terms in the ‘category’ taxonomy.

add_action('created_category', 'set_default_parent_term', 10, 4);

function set_default_parent_term($term_id, $tt_id, $taxonomy, $args) {
    $default_parent_term_id = 5;
    if (!$args['parent']) {
        wp_update_term($term_id, $taxonomy, array('parent' => $default_parent_term_id));
    }
}

Redirect to term editing page after creating a new term

Redirect the user to the term editing page after creating a new term in the ‘category’ taxonomy.

add_action('created_category', 'redirect_to_term_edit_page', 10, 4);

function redirect_to_term_edit_page($term_id, $tt_id, $taxonomy, $args) {
    $term_edit_url = get_edit_term_link($term_id, $taxonomy);
    wp_redirect($term_edit_url);
    exit;
}

Add a custom field to new terms

Automatically add a custom field named ‘color’ with a default value ‘blue’ to new terms in the ‘category’ taxonomy.

add_action('created_category', 'add_custom_field_to_new_term', 10, 4);

function add_custom_field_to_new_term($term_id, $tt_id, $taxonomy, $args) {
    $default_color = 'blue';
    add_term_meta($term_id, 'color', $default_color, true);
}