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).

<?php
function xg_edit_featured_category_field( $term ){
    $term_id = $term->term_id;
    $term_meta = get_option( "taxonomy_$term_id" );
?>
<tr class="form-field">
  <th scope="row">
    <label for="term_meta[featured]"><?php echo _e('Home Featured') ?></label>
  <td>
    <select name="term_meta[featured]" id="term_meta[featured]">
      <option value="0" <?=($term_meta['featured'] == 0) ? 'selected': ''?>><?php echo _e('No'); ?></option>
      <option value="1" <?=($term_meta['featured'] == 1) ? 'selected': ''?>><?php echo _e('Yes'); ?></option>
    </select>
  </td>
  </th>
</tr>
<?php
} 


add_action( 'category_edit_form_fields', 'xg_edit_featured_category_field' );

// Save the field
function xg_save_tax_meta( $term_id ){ 
  if ( isset( $_POST['term_meta'] ) ) {
      $term_meta = array();
      // Be careful with the intval here. If it's text you could use sanitize_text_field()
      $term_meta['featured'] = isset ( $_POST['term_meta']['featured'] ) ? intval( $_POST['term_meta']['featured'] ) : '';
  // Save the option array.
      update_option( "taxonomy_$term_id", $term_meta );
  }
} // save_tax_meta
add_action( 'edited_category', 'xg_save_tax_meta', 10, 2 );

add_action( 'category_add_form_fields', 'xg_edit_featured_category_field' );

// Add column to Category list
function xg_featured_category_columns($columns) {
  return array_merge($columns, array('featured' =>  __('Home Featured')));
}

add_filter('manage_edit-category_columns' , 'xg_featured_category_columns');

// Add the value to the column
function xg_featured_category_columns_values( $deprecated, $column_name, $term_id) {
  if($column_name === 'featured'){ 
    $term_meta = get_option( "taxonomy_$term_id" );
    if($term_meta['featured'] === 1){
      echo _e('Yes');
    }else{
      echo _e('No');
    }
  }
}
add_action( 'manage_category_custom_column' , 'xg_featured_category_columns_values', 10, 3 );

About Rick

Senior Front-end Software Engineer from Barcelona, Haidong Gumdo Instructor (korean martial art of the sword), street photographer, travel lover, TV addict, Boston Red Sox fan, and privacy advocate.
23 comments
  • Awesome post man, your my hero!!!

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

    Thanks again!

  • 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?

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

  • 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

  • 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?

  • 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.


    // Add the value to the column

    function xg_featured_category_columns_values( $deprecated, $column_name, $term_id) {

    if($column_name === 'featured'){

    $term_meta = get_option( "taxonomy_$term_id" );

    switch ($term_meta['featured']){
    case "0":
    echo "0";
    break;

    case "1":
    echo "1";
    break;

    case "2":
    echo "2";
    break;

    case "3":
    echo "3";
    break;

    case "4":
    echo "4";
    break;

    case "5":
    echo "5";
    break;

    case "6":
    echo "6";
    break;

    case "6":
    echo "6";
    break;

    case "7":
    echo "7";
    break;

    case "8":
    echo "8";
    break;

    case "9":
    echo "9";
    break;

    case "10":
    echo "10";
    break;

    default:
    echo "None";
    }
    }
    }

    add_action( 'manage_category_custom_column' , 'xg_featured_category_columns_values', 10, 3 );

  • 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.


    function xg_edit_featured_category_field( $term ){
    $term_id = $term->term_id;
    $term_meta = get_option( “taxonomy_$term_id” );
    ?>

  • 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!

  • 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.


    if ( get_field( 'my_key' ) == sunny ) {
    get_template_part( 'single/single', 'modern' );
    }

    Waiting for your quick reply! Thanks!!

  • 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?

Leave a Reply

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

*
*