Family Encyclopedia >> Electronics

How to add custom metadata fields to custom taxonomies in WordPress

By default, WordPress taxonomies (categories, tags, etc.) have name, slash, parent, and description fields. Recently, while working on a client project, we found the need to add custom metafields to custom taxonomies. We needed a way to add custom text on each taxonomy archive page. One way would be to encode the text using the conditional statements in our taxonomy-name .php file. That would be a very inefficient way to do it, and it wouldn't give our client the ability to modify the text in the future. So we decided to future test the situation by adding custom metafields to custom taxonomies. In this article, we'll show you how to add additional custom metafields to custom taxonomies.

Note:This tutorial is for designers and developers.

While searching for an efficient method, we found Pippin's tutorial that shows you how to do it. While his tutorial was great, he forced us to write a lot of code. We decided to search a bit more to see if anyone has created an easier way to do this. Maybe a plugin or a class. Fortunately, we found a solution from Ohad Raz on Github. After going through the same problem, he decided to write a class to make it easier for everyone else (he has to love the WordPress community). Thanks ohad.

In our case, we decided to add this functionality as a plugin instead of a theme. You can choose the method you like the most. For the sake of this tutorial, we'll go the plugin route.

The first thing you need to do is download the tax class from Github. Create a new folder and name it "taxonomy fields". Save the "Tax-meta-class" folder inside that folder.

The zip comes with a file called class-use-demo.php. Just rename that file and call it taxonomy-fields.php

Ohad did a great job documenting the file, so it's pretty self explanatory. He has examples of all kinds of fields that he can add (text field, textarea, checkbox, select, radio, date, time, color picker, file upload, etc.). You don't have to use all the fields. Just get rid of the ones you don't want.

Once you are done adding the fields, upload the taxonomy fields folder to your plugins folder. Activate the plugin and add data in your fields.

Now you're ready to display these additional fields in your taxonomy template. Open your taxonomy template. This would be something like taxonomy- taxonomy-name .php file. There, you can simply add the following:

 term_id, 'text_field_id'); echo $ saved_data; ?> 

That's it. These classes make it really easy and improve your workflow. We hope this tutorial helped you add custom metafields to custom taxonomies.