The create_{$taxonomy} WordPress PHP action fires after a new term is created for a specific taxonomy. The dynamic portion of the hook name, $taxonomy
, refers to the slug of the taxonomy the term was created for. Possible hook names include create_category
and create_post_tag
.
Usage
add_action('create_{$taxonomy}', 'your_custom_function', 10, 3); function your_custom_function($term_id, $tt_id, $args) { // your custom code here return $term_id; }
Parameters
$term_id
(int): Term ID.$tt_id
(int): Term taxonomy ID.$args
(array): Arguments passed towp_insert_term()
.
More information
See WordPress Developer Resources: create_{$taxonomy}
Examples
Log new term creation
Log the creation of a new term in a custom log file.
add_action('create_category', 'log_new_term_creation', 10, 3); function log_new_term_creation($term_id, $tt_id, $args) { error_log("New term (ID: {$term_id}, Taxonomy ID: {$tt_id}) created with arguments: " . print_r($args, true)); }
Notify admin of new term
Send an email to the admin when a new term is created.
add_action('create_post_tag', 'notify_admin_new_term', 10, 3); function notify_admin_new_term($term_id, $tt_id, $args) { $admin_email = get_option('admin_email'); $term = get_term($term_id); $subject = "New term '{$term->name}' created"; $message = "A new term '{$term->name}' (ID: {$term_id}, Taxonomy ID: {$tt_id}) has been created."; wp_mail($admin_email, $subject, $message); }
Assign a new term to a custom post type
Automatically assign the new term to a custom post type when created.
add_action('create_category', 'assign_new_term_to_custom_post_type', 10, 3); function assign_new_term_to_custom_post_type($term_id, $tt_id, $args) { $custom_post_type = 'your_custom_post_type'; $term = get_term($term_id); wp_set_object_terms($term_id, $term->slug, $custom_post_type, true); }
Set a default parent term for a custom taxonomy
Automatically set a default parent term for a new term in a custom taxonomy.
add_action('create_your_custom_taxonomy', 'set_default_parent_term', 10, 3); function set_default_parent_term($term_id, $tt_id, $args) { $default_parent_term_id = 123; // Replace with your default parent term ID wp_update_term($term_id, 'your_custom_taxonomy', array('parent' => $default_parent_term_id)); }
Create a custom field for a new term
Automatically create a custom field for the new term when it’s created.
add_action('create_post_tag', 'create_custom_field_for_new_term', 10, 3); function create_custom_field_for_new_term($term_id, $tt_id, $args) { $custom_field_key = 'your_custom_field_key'; $custom_field_value = 'your_custom_field_value'; add_term_meta($term_id, $custom_field_key, $custom_field_value, true); }