The created_{$taxonomy} WordPress PHP action fires after a new term in a specific taxonomy is created, and after the term cache has been cleaned. The dynamic portion of the hook name, $taxonomy, refers to the taxonomy slug.
Usage
add_action('created_{$taxonomy}', 'your_custom_function', 10, 3);
function your_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 towp_insert_term().
More information
See WordPress Developer Resources: created_{$taxonomy}
Examples
Update post count after a new category is created
This example updates the post count for a custom post type when a new category is created.
add_action('created_category', 'update_post_count', 10, 3);
function update_post_count($term_id, $tt_id, $args) {
// Update post count for custom post type
update_post_count('your_custom_post_type');
}
Send an email when a new post tag is created
This example sends an email to the administrator when a new post tag is created.
add_action('created_post_tag', 'email_on_new_post_tag', 10, 3);
function email_on_new_post_tag($term_id, $tt_id, $args) {
$admin_email = get_option('admin_email');
$subject = 'New Post Tag Created';
$message = 'A new post tag has been created on your website.';
wp_mail($admin_email, $subject, $message);
}
Log new term creation
This example logs the creation of new terms in a custom taxonomy called ‘book_genre’.
add_action('created_book_genre', 'log_new_term', 10, 3);
function log_new_term($term_id, $tt_id, $args) {
$log_message = 'New term created with term ID: ' . $term_id . ' and taxonomy ID: ' . $tt_id;
error_log($log_message);
}
Assign a parent term when a custom taxonomy term is created
This example assigns a parent term to newly created terms in a custom taxonomy called ‘product_type’.
add_action('created_product_type', 'assign_parent_term', 10, 3);
function assign_parent_term($term_id, $tt_id, $args) {
$parent_term_id = 123; // The ID of the parent term
wp_update_term($term_id, 'product_type', array('parent' => $parent_term_id));
}
Notify users when a new term is created in a custom taxonomy
This example sends an email to all users when a new term is created in a custom taxonomy called ‘event_type’.
add_action('created_event_type', 'notify_users_new_term', 10, 3);
function notify_users_new_term($term_id, $tt_id, $args) {
$users = get_users();
$subject = 'New Event Type Created';
$message = 'A new event type has been created on the website.';
foreach ($users as $user) {
wp_mail($user->user_email, $subject, $message);
}
}