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. In 2011, our friend Pippin released a tutorial on adding custom meta fields to taxonomies. We thought it was time to post an updated tutorial now that custom meta fields aren’t such a nightmare.

Much like Pippin’s example, 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

“`php
function my_taxonomy_add_meta_fields( $taxonomy ) {
?>


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 {$taxonomy_name}_add_form_fields. This pattern will persist throughout the process.

The Edit Term Page

“`php
function my_taxonomy_edit_meta_fields( $term, $taxonomy ) {
$my_field = get_term_meta( $term->term_id, ‘my_field’, true );
?>

Show Full Content
Previous Editor Wars
Next What’s This?

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Close
Close