Add Metadata to Categories – WordPress

If you’re reading this chances are you have the same problem I had.

How do I add custom metadata to a category?

I needed to add a simple dropdown to categories in order to set them as featured. Then I wanted to use this featured field to show them in the home page in a specific location.

Well, after a lot of reading I didn’t find any blog post or tutorial that was clean. I’ve found plugins and big chunks of code, unbearable.

I’ll do my best to explain you the main functions you need to achieve this:

Custom Metadata Category WordPress

Plugin or functions.php?

First of all you need to decide if you want to create a plugin or add this code to your functions.php file. For my project I decided to use the functions.php since it’s a custom code specific for this project, unlikely to be reused in the same way.

How to use the bits of code below?

I’ll explain part by part but you can copy them all and paste them in order inside your functions.php file, I’ll probably work (at the time of the writing we had WordPress 3.8).

Add your new field to the Edit form

We’ll add a new TR with our dropdown. Pay special attention to the select name and id, which is called: term_meta[featured] (our new metadata field is featured).

So the code above does nothing on it’s own, we need to use add_action to hook it up:

Right now, if you Edit a category you’ll see something like this:

Edit Category Custom Metadata WordPress

Save the metadata

So basically now we have a form which doesn’t get stored in the database. Not very useful, right?

We need to tell WordPress to save this custom metadata on the save action, if he finds it:

With this, the Edit category process works.

What about the Create Category Form?

You can also add the field to the create form. However the HTML wrappers are not the same: for the edit form you wrap the fields on a TR but in the create form you need a DIV.

Because I didn’t want to duplicate my HTML field I just didn’t add it to the create form. In case you want to do it, here’s how:

Custom Columns for the Category list

This final step is optional, perhaps you don’t need it. However, I think it’s nice to present this new field in the category list (like in the first screenshot at the top).

How do we do that?

First of all add the column header:

This code just ouputs the Home Featured column, with no values in it.

With the following snippet we’ll retrieve the metadata from the database:

That’s it. I hope I explained myself, make sure to check the WordPress Codex if you have any issue with the functions.

23 Responses to “Add Metadata to Categories – WordPress”

  1. Jared July 24, 2014 at 9:35 am #

    Awesome post man, your my hero!!!

    I just have one question, how do I retrieve the data on the front-end?

    Thanks again!

    • Rick July 24, 2014 at 9:45 am #

      Hi Jared,

      Use the get_option function. Here’s a snippet following my post example:

      Useful links:

      http://codex.wordpress.org/Function_Reference/get_the_category
      http://codex.wordpress.org/Function_Reference/get_option

      Cheers,

      Rick

      • Jared July 24, 2014 at 9:49 am #

        Thanks again Rick, I just stumbled across “get_option”, when looking in the database for the new meta 🙂

        Your a legend – appreciate the help!

      • Rick July 24, 2014 at 9:50 am #

        You’re welcome Jared!

  2. Alex September 19, 2015 at 5:27 pm #

    Thank you, man

  3. Anjuman Chawla September 22, 2015 at 9:13 am #

    I have written almost the same code as that of your but the function call in create_category hook is not getting called.The same function is working good for edited_category hook.Can you advice something?

    • Rick September 22, 2015 at 9:35 pm #

      I’m afraid I wouldn’t know.

  4. MaxClowReed February 9, 2016 at 10:19 pm #

    I have another question, how you can sae the info of Input text fields instead a Select? Thanks a lot.

    • Rick February 13, 2016 at 8:52 pm #

      Hey there, I’m not sure I understand your question.

  5. TNL Solutions March 1, 2016 at 9:31 am #

    Hi Rick,

    As you guide, extra field currently saved to wp-options, however, if I want to sort categories taxonomies base on option values , how can I do ?

    Thanks,

    Thang

  6. Sunny Anand March 10, 2016 at 2:24 am #

    Hi! Thanks for this great article! It really made my day! once again thanks for this free treat!

    So far its working just awesome! But, I have come to a hold and maybe you can help me here.

    I have 10 options in the dropdown field, and the dropdown field is working just fine. but, how do I show the value on the column? currently, its showing “No”, because of the “if” statement I have in the code for “Yes” and “No”. And in my case I have 10 options so, I guess you would help me figure out how do I show the 10 options as saved by each category on the column?

  7. Sunny Anand March 10, 2016 at 3:09 am #

    Woww!! Excellent and Marvellous!! I just did it!

    All thanks to you and some credit goes to http://www.tutorialspoint.com/php/php_decision_making.htm for the answer to my question!

    now I have code that’s working just excellent. I am posting it here so that maybe future users can benefit from it too.

  8. Sunny Anand March 10, 2016 at 3:22 am #

    Hey, sorry for posting twice here but I think some codes are being erased while posting!! Thats strange. ok, no problem I will post it here as a simple texts.

  9. Sunny Anand March 10, 2016 at 3:25 am #

    OMG!! sorry! Again it erased my codes while posting comments here. please delete my half posted comments, as it will be of no use to anyone.

    I hope atleast I made you understand how I did it!!

    Again, lots of thanks for your grand code and this awesome article!

    • Rick March 10, 2016 at 8:18 pm #

      I’m glad it all worked out!

      Regards

      • Sunny Anand March 16, 2016 at 12:41 pm #

        All thanks to you! Cheers!!

  10. Sunny Anand March 16, 2016 at 12:55 pm #

    Okay, I have one more question, which I am trying to solve from more than 10 hours but with no success!! I am sure you will help me out here.

    1. How do I display the field on the frontend?

    2. Say, I want to pass an argument to the page, for example, I have this code in my hand which is a normal custom field of a single post in working condition, and I want to achieve a similar solution through your code.

    Waiting for your quick reply! Thanks!!

    • Sunny Anand March 16, 2016 at 1:06 pm #

      And ofcourse, I am using your code in my archive.php page to redirect users to some other pages based on what the admin’s choice is on the category page!

      And one more thing, how do I output the field classes of your code in my body tag?

      For example, I have this code and I want to achieve similar solution.

      Any idea would be very much appreciated. Thanks!

  11. Daniel April 1, 2016 at 10:39 am #

    Great tutorial – i do have a problem while saving.

    “Warning: Cannot modify header information – headers already sent by” after updating the form. You have an idea?

    • Rick April 5, 2016 at 1:21 pm #

      Check that both start and end of the file do not have whitespaces.

Leave a Reply

Add <code> Some Code </code> by using this tags.