WordPress adds fields to the Custom Taxonomy Terms project
WordPress provides meta-field support for almost everything: article types, users, comments, blogs on a multi-site network, and custom taxonomy items/taxonomy terms (Taxonomy Terms).
Here we explain proper terms Taxonomy Terms, the Chinese translation of “Classification Terms” is a bit difficult to understand. In fact, it refers to a classification item under a certain classification. For example, the articleclassification catalogueandlabel, are two default classifications, any classification item or label in them is Taxonomy Term。WordPress also allows us to register new classifications, soTaxonomy TermsIt also includes classification items for this custom classification.
In the following example, we will add a new field, Text Field, to the tag Tags.
article directories
1. Add fields to the Add New Term interface
To add fields on the Add New XX interface, we will use action hooks{Taxonomy}_add_form_fields
, all we have to do is how to display the fields.
add_action( 'post_tag_add_form_fields', 'misha_add_term_fields' );
function misha_add_term_fields( $taxonomy ) {
echo '<div class="form-field">
<label for="misha-text">Text Field</label>
<input type="text" name="misha-text" id="misha-text" />
<p>Field description may go here.& lt;/p>
</div>';
}
- I decided to
post_tag
Add a field to the taxonomy, so my action hook ispost_tag_add_form_fields
。However, you can use any custom classification name here. $taxonomy
The taxonomy name is provided as a variable inside the function.- Don’t forget to check in the
div
addedform-field
Class.
The results are as follows:

post_tag_add_form_fields
Action hooks add fields to the Add New Tag page.2. Add fields to the Edit Terms interface
Very similar to the previous part of this tutorial, the main difference is that if metadata appears, this field must be filled in.
Let’s start with an action hook{Taxonomy}_edit_form_fields
。forpost_tag
Classification, it will bepost_tag_edit_form_fields
, if it is a custom classification, you need to modify the previous part, such asmisha_taxonomy_edit_form_fields
。
add_action( 'post_tag_edit_form_fields', 'misha_edit_term_fields', 10, 2 );
function misha_edit_term_fields( $term, $taxonomy ) {
$value = get_term_meta( $term->term_id, 'misha-text', true );
echo '<tr class="form-field">
<th>
<label for="misha-text">Text Field</label>
</th>
<td>
<input name="misha-text" id="misha-text" type="text" value="' . esc_attr( $value ) . '" />
<p class="description">Field description may go here.& lt;/p>
</td>
</tr>';
}
- The function takes two parameters:
$term
– is the object of the term currently edited,$taxonomy
– Classification name. - we’re
get_term_meta()
Functions are used here to obtain terminology metadata. - don’t forgetescapeData obtained from a database.
Now the editing interface is as follows:

post_tag_edit_form_fields
Action hooks add fields to the Edit Tags page.3. save field
The final step is to save the field values in the database. We also have two action hooks- created_{Taxonomy}
andedited_{Taxonomy}
。Fortunately, we can connect the same callback function to both functions.
add_action( 'created_post_tag', 'misha_save_term_fields' );
add_action( 'edited_post_tag', 'misha_save_term_fields' );
function misha_save_term_fields( $term_id ) {
update_term_meta(
$term_id,
'misha-text',
sanitize_text_field( $_POST[ 'misha-text' ] )
);
}
It’s done here!
Note: The text content comes from: Misha Rudrastyh blog, translated and compiled by WordPress University.