How can I add custom meta fields in categories?

cooljaz124 picture cooljaz124 · Nov 10, 2010 · Viewed 9.1k times · Source

Does anyone have any idea how to add custom meta fields while making categories and fetch them in the loop in WordPress? I was wondering how to do that without hacking the WordPress core, but if I do – it won't become a hindrance to update WordPress in the future.

A plugin I have found that comes close is Wp-Category-Meta, but it doesn't have the ability to add checkboxes as fields in Edit Categories.

screenshot

This will be very useful as users can make certain categories "featured", and then the code can use that meta value in the loop to style "featured" categories differently.

Answer

jave.web picture jave.web · Mar 16, 2016

The problem: Wordpress does not have a structure nor method to store "meta" values for taxonomies.

UPDATE 2017: WP 4.4+ has "term meta"!

For working with term metas use these:
update_term_meta()
get_term_meta()
delete_term_meta()
add_term_meta()

The Actions below are still valid though! :)

Additional reading: 4.4 Taxonomy Roundup

Solution for WP version <= 4.3.x and COMMON actions

Actions:

  1. create_category and edit_category for category edit
  2. category_add_form_fields and category_edit_form for category form fields

There are more actions than I've presented, but they seem to be deprecated (according to developer.wordpress.org).

The reason I chose the actions that I chose:
- They work on WordPress 4.4.2
- Due to lack of documentation I assumed these are the new ones replacing the deprecated ones...

Functions:

  1. get_option( $option, $default );
  2. update_option( $option, $new_value, $autoload );

update_option has two great abilities:
a) It craetes the option when such option does not exist yet

Unless you need to specify the optional arguments of add_option(), update_option() is a useful catch-all for both adding and updating options.

b) $new_value can be an integer, string, array, or object.
You may ask, why to use array/object? ...well, because each option = 1 database row => you probably want to store your category options in one row :)

The CODE

  function my_category_form_fields($tag_object){
    //output/display extra form fields, e.g. by echo ...
    //ADD EXTRA SPECIFIC FIELD TO LATER CHECK IF IT'S CATEGORY SAVE/EDIT!
    //(see note at 'edit_category' action...)

    if( !empty($tag_object['term_id']) ){
      //edit category form specific
      //...load existing options with get_option( $option, $default );
    } else {
      //create category form specific
    }
  }

  function my_category_save(){
    //CHECK FOR YOUR EXTRA SPECIFIC FIELD TO CHECK IF IT'S CATEGORY SAVE/EDIT
    //(see note at 'edit_category' action...)

    //SECURITY CHECK
    if( empty($_POST['EXTRA_SPECIFIC_FIELD']) || ! current_user_can('manage_categories') )
       return null;

    //save your form values using update_option()
    //Recommendation:
    //Add "category_" prefix and $category_id to your option name!
  }

  add_action( 'create_category', 'my_category_save', 10, 1 ); 

  //Runs when a category is updated/edited,
  //INCLUDING when a post or blogroll link is added/deleted or its categories are updated
  //(which causes the count for the category to update)
  add_action( 'edit_category',   'my_category_save', 10, 1 ); 

  add_action( 'category_add_form_fields', 'my_category_form_fields', 10, 1 ); 
  add_action( 'category_edit_form',       'my_category_form_fields', 10, 1 ); 

Create or Edit?

You might wonder whether you are creating or saving a category - this not documented yet (as far as I know), but from testing:

  1. Edit save => $tag_object is object and contains some properties, most notably:
    • term_id
    • taxonomy
    • filter
  2. Create save => $tag_object is just a regular string "category" - I guess this might change in the future...

General taxonomy

There are also actions like these for taxonomies in general - check these actions.