Assign a color to a category in WordPress


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.

Enqueue wp-color-picker

That’s it.

Introduction to WordPress term meta


Author: Leo

My name is Leo who's behind this blog. I've been working with wordpress since 2010. I'll write about themes, plugins, tutorials and all about wordpress here. Hope you find it useful!

1 thought on “Assign a color to a category in WordPress”

Leave a Reply

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