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
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
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: {$taxonomy_name}_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_{$taxonomy_name}
and edited_{$taxonomy_name}
. And that’s all there is to it! (Or is it…?)
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.
This should be fairly self-explanatory. Simply hook the manage_edit-{$taxonomy_name}_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!
The manage_{$taxonomy_name}_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! If you have any questions or comments, please feel free to leave a comment below, and happy coding!