In a previous post I’ve showed how to add a like button in category/tag/etc archive page with the term meta function introduced in WordPress 4.4.
I’ve also had another post about how to add additional fields to Category and Taxonomy. Since it’s posted before WordPress 4.4, the data of custom fields are saved using a different way.
Today I am going to show you how to save the data by leveraging the term meta functions. To make things easier, let’s assign a color to a category.
Register term meta
First, we need to register the meta with
register_meta() (See codex) function.
jt_sanitize_hex is a sanitize callback function for when meta is saved. What the function is going to do is simply make sure that we have a valid color hex code.
Get term meta
Add form fields
Because add new term and edit term forms have different HTML markup, so we need two separate callback functions for handling output of the fields.
Save term meta
Add a term meta column
By now it’s almost done. But we don’t want to manually type a hex codes. Instead, we’ll add a color picker box.
Introduction to WordPress term meta