I Doubled my Income in a Year

WordPress allowed me to get a better job & better projects in just a Year. I will give you tips, tricks and guide you into WordPress development so you can get more income. Sign up to my 7-day email course!

Orange with Tutorial Text

How to Add Custom Fields to WordPress Taxonomies

WordPress Taxonomies have by default fours field you may enter: title, slug, parent and description. How about adding an image to a category? You can do that with custom fields. In this tutorial we will learn how to add custom fields to custom taxonomies.

Hooks for Custom Fields on Taxonomies

WordPress provides us with two hooks for creating custom fields that will show when adding or editing new terms to our taxonomies. This taxonomies can also be the default categories or tags.

Creating the Term

In the screen where we create the term, we have an action hook:

Here we can use the $taxonomy parameter in our own function. This can be used for an additional check if we are on the correct taxonomy screen.

Editing the Term

When we have already created a new term and we want to edit it, we will be on another screen. This screen is using another action hook for adding custom fields to taxonomies.

This hook will provide us with 2 parameters: $tag and $taxonomy. The first, $tag, is the object from which we will get the id of our term. This can be then used to get the current value for our field.

Saving the Term

In both scenarios (creating or editing) we need to save our new value in order to use it across the site. There are two different hooks for those scenarios when saving, but we can hook the same function for saving the field.

To use those hooks with our own custom taxonomy or some other existing taxonomies (categories or tags), we need to replace the {$taxonomy} with our registered taxonomy name.

Let’s now see a real-world scenario.

Adding an Image Field to Categories

In this example, we will add an image field to the default categories. This field will be a simple input field which you can then use to enter a link to an image. We could extend this with a custom button and some custom JavaScript to enable the WordPress Media Uploader for this field, but that is something for another tutorial.

Creating a Category

First, we will add our image field to the category on the screen which is used when creating a new category:

Each field on this screen is wrapped with a div.form-field so we are using the same HTML structure here.

Editing a Category

On this screen, all the fields are wrapped inside a table row tr with th for the label and td for the field. We have access to our term object so we can get the term ID to get the image field.

To get the image field we are using the function get_term_meta which is very similar to the function get_post_meta. Read more about that function on WordPress Code Reference.

Saving the Image

The last step is to create a function that will save the link that is added to our field:

In this function we are checking if our image field is posted using the global variable $_POST. If that is true, we are getting the link to the image and we are saving it using the function update_term_meta. You can learn more about that function here.

Use the Term Meta and Custom Fields

As a conclusion, I want to say that you should use the term meta and custom fields when you are working on a custom solution. This can be a plugin or something else. As an example, you could create a taxonomy Locations. This taxonomy can have a field which will show a Google Map where you can point the location of the term (place) or enter an address to it.

You see my point? If you have ever worked with custom fields on taxonomies, I would appreciate it if you would share your experience or examples on this topic! 

About the Author Igor Benic

Web Developer who mainly uses WordPress for projects. Working on various project through Codeable & Toptal. Author of several ebooks at https://leanpub.com/u/igorbenic.

follow me on:

EDD for Developers

In less than 2 weeks, I have earned $1,800 by working on a custom solution with Easy Digital Downloads without any prior experience. Let me teach you how you can too with various custom solutions.

Get the eBook

Leave a Comment:

How to Add Custom Fields to WordPress Taxonomies

by Igor Benic time to read: 3 min
0
Complete course to Become a WordPress DeveloperJoin the Course
+