There are numerous plugins available that add custom meta fields to posts, pages, and the like. Equally numerous are the tutorials walking developers through how to do so. The one thing that isn't commonly taught, however, is how to add custom meta fields to taxonomies. This oversight is in no small part due to how difficult custom meta fields for taxonomies was until the release of WordPress 4.4.
Adding a custom meta field to a taxonomy still requires three functions: one to add the field to the Add New page, one to add the field to the Edit Term page, and one to save the value of the custom field from both pages.
The Add New page
function my_taxonomy_add_meta_fields( $taxonomy ) {
?>
<div class="form-field term-group">
<label for="my_field"><?php _e( 'My Field', 'my-plugin' ); ?></label>
<input id="my_field" name="my_field" type="text" />
</div>
<?php
}
add_action( 'my_taxonomy_add_form_fields', 'my_taxonomy_add_meta_fields', 10, 2 );As you can see, this is a straightforward function - no need to deal with pre-saved values, just a simple HTML form. We should note that, while our example doesn't demonstrate it, if you are adding more than one field it might be a good idea to make the name of the input an array. This array would allow us to iterate through the array on saving rather than having to save each value individually.
Take note of the action we are adding the function to. It takes the form
_add_form_fields. This pattern will persist throughout the process.
The Edit Term page
function my_taxonomy_edit_meta_fields( $term, $taxonomy ) {
$my_field = get_term_meta( $term->term_id, 'my_field', true );
?>
<input id="my_field" name="my_field" type="text" value="<?php echo esc_attr( $my_field ); ?>" />
<?php
}
add_action( 'my_taxonomy_edit_form_fields', 'my_taxonomy_edit_meta_fields', 10, 2 );Much like the Add New page, this is a simple form. The only differences are the
addition of the $term parameter being passed to the function, the retrieving of
the meta value, and echoing that value in the field. Also similar to the
Add New page is the action hook: _edit_form_fields.
Saving your custom meta
This should look familiar; it's the same as saving any other meta. The one
thing that's different is that unlike post meta, we have to use two separate
hooks for creating and updating meta. These hooks are created_ and
edited_. And that's all there is to it! (Or is it...?)
function my_taxonomy_save_taxonomy_meta( $term_id, $tag_id ) {
if ( isset( $_POST['my_field'] ) ) {
update_term_meta( $term_id, 'my_field', esc_attr( $_POST['my_field'] ) );
}
}
add_action( 'created_my_taxonomy', 'my_taxonomy_save_taxonomy_meta', 10, 2 );
add_action( 'edited_my_taxonomy', 'my_taxonomy_save_taxonomy_meta', 10, 2 );Doing more with custom meta
So now that we've created our custom meta field, what if we wanted to display that data in the taxonomy table? It's quite simple! Doing so requires two functions: one to add the column, and one to display the data in each row.
function my_taxonomy_add_field_columns( $columns ) {
$columns['my_field'] = __( 'My Field', 'my-plugin' );
return $columns;
}
add_filter( 'manage_edit-my_taxonomy_columns', 'my_taxonomy_add_field_columns' );This should be fairly self-explanatory. Simply hook the manage_edit-_columns
filter and add your column (or columns) to the array. What you use for the
index is irrelevant, but for the sake of simplicity, we've chosen to use the
field name for the column index. Once you have a column defined, you have to
populate it!
function my_taxonomy_add_field_column_contents( $content, $column_name, $term_id ) {
switch( $column_name ) {
case 'my_field' :
$content = get_term_meta( $term_id, 'my_field', true );
break;
}
return $content;
}
add_filter( 'manage_my_taxonomy_custom_column', 'my_taxonomy_add_field_column_contents', 10, 3 );The manage__custom_column filter takes three arguments: $content (the
content for the field), $column_name (the name of the column), and $term_id
(the ID of the term; i.e.: the row we are working with). We only want to edit
the content for our custom field, hence the switch statement. In our example,
we've chosen to simply output the value of the custom meta field, but you can
do pretty much anything with the retrieved data.
That's all folks! Adding custom meta fields to taxonomies is no longer a headache! Happy coding!
