created_(taxonomy)
Fires after a term (taxonomy item) is added.
For example, this hook is a convenient place to save term meta fields from the term creation page.
The dynamic $taxonomy part of the hook name is the name (slug) of the taxonomy being handled.
See also the identical created_term hook, where the taxonomy name is passed in the third parameter. Otherwise, the hook works the same way.
Usage
add_action( 'created_(taxonomy)', 'wp_kama_created_taxonomy_action', 10, 3 );
/**
* Function for `created_(taxonomy)` action-hook.
*
* @param int $term_id Term ID.
* @param int $tt_id Term taxonomy ID.
* @param array $args Arguments passed to wp_insert_term().
*
* @return void
*/
function wp_kama_created_taxonomy_action( $term_id, $tt_id, $args ){
// action...
}
- $term_id(int)
- ID of the term (taxonomy item).
- $tt_id(int)
- Unique term identifier that accounts for the taxonomy (in recent WordPress versions, it is equal to the term ID; see Taxonomies in WordPress for details).
- $args(array) (WP 6.1)
- Arguments passed to wp_insert_term().
Examples
#1 Add a custom field to the term creation form for the category taxonomy
add_action( 'category_add_form_fields', 'add_custom_form_field' );
// Add a custom field to the term creation form.
function add_custom_form_field( $term ) {
?>
<div class="form-field">
<label for="custom-field">
<?php _e( 'Custom field' ); ?>
</label>
<input id="custom-field" type="text" name="custom_field" value="<?php esc_attr_e( get_term_meta( $term->term_id, 'custom_field', true ) ) ?>">
<p><?php _e( 'Custom field description' ); ?></p>
</div>
<?php
}
add_action( 'created_category', 'save_custom_form_field' );
// Save the custom field value.
function save_custom_form_field( $term_id ) {
if ( ! isset( $_POST['custom_field'] ) ) {
return;
}
if ( ! current_user_can( 'edit_term', $term_id ) ) {
return;
}
if ( ! wp_verify_nonce( $_POST['_wpnonce_add-tag'], 'add-tag' ) ) {
return;
}
update_term_meta( $term_id, 'custom_field', wp_unslash( $_POST['custom_field'] ) );
}Changelog
| Since 2.3.0 | Introduced. |
| Since 6.1.0 | The $args parameter was added. |
Where the hook is called
created_(taxonomy)
wp-includes/taxonomy.php 2782
do_action( "created_{$taxonomy}", $term_id, $tt_id, $args );