Taxonomy Custom Meta Fields

Adding custom meta fields to taxonomies can be useful for associating images, icons or more information to custom taxonomy terms in WordPress. Custom meta fields can also be added to posts, pages and custom post types.

Three functions can be added to your WordPress theme’s functions.php

The first function the term’s custom meta field to the ‘add new term’ page.

 

Note the add_action() parameters.

 

You can tweak the first parameter {taxonomy_name}_add_form_fields to set what taxonomy the field gets added to. In my examples, I will be using the “category” taxonomy.

Next we need to add the function that displays the custom meta field on the ‘edit taxonomy term’ page.

 

Finally, we need to add the last function, which saves all the data we have inputted into the fields.

 

As we did in step one, make sure you modify “category” from the add_action() parameters at each step, to reflect the taxonomy you want the field displayed in.

Reference: Pippin

To display the data on the frontend:

 

  • Testzap Zaptest

    I want to use this functionality for woo commerce product category. How can I implement this

  • nonbrake

    I want to use this functionality for woo commerce product category, please help.

  • http://baguiogreatdeals.com litopj

    thanks, it works perfectly

  • http://baguiogreatdeals.com litopj

    change “category_add_form_fields” to “product_cat_add_form_fields”;
    “category_edit_form_fields” to “product_cat_edit_form_fields”;
    “edited_category” to “edited_product_cat”;
    “create_category” to “create_product_cat”

  • nonbrake

    It works, I changed input to textrarea

    but I can´t show metafield on the frontend.

  • http://baguiogreatdeals.com litopj

    if you are using it in a custom taxonomy template same as the example above:

    $metafieldArray = get_option(‘taxonomy_’. $term->term_id);
    $metafieldoutput = $metafieldArray[‘custom_term_meta’];
    echo $metafieldoutput;

  • nonbrake

    I would like to use for woocommerce category (product_cat), but it doesn´t work. This works (put in archive-product.php)

    Anyone having problems retrieving the values on a custom taxonomy archive page may find the following useful:

    $t_id = get_queried_object()->term_id;
    $term_meta = get_option( "taxonomy_$t_id" );
    echo $term_meta['custom_term_meta'];

  • Tapmeet Singh

    Hi, Thank you so Much!!

    There is one thing more is there a way to show these fields only in Sub category(Child)….

    I mean if suppose we have a car manufacturer called Mercedes and child are C220 and C320. Then is there a way to show these fields only on Child Category Page in C220 and C320 Only?

    Thanks In advance!!

  • nonbrake

    Hello,
    I create custom field for taxonomy product_cat (product category for Woocommerce).
    1) How can I change/add priority of this custom field in administration - I mean position of custom field in administration category between other fields (under/above the term-description). For meta fields you can use the priority = normal/high/ … and here?

    2) How can I add the wp-editor to this custom filed? I tried add editor (tinymce) using wp-editor ( http://codex.wordpress.org/Function_Reference/wp_editor ) or jQuery but without success.

    Thanks

  • nonbrake

    Answer to my questions
    1) Priority - only change number in add action
    add_action( ‘product_cat_edit_form_fields’, ‘xxx_taxonomy_edit_meta_field’, 20, 2 );

    2) http://www.wpmusketeer.com/add-a-wysiwyg-field-to-woocommerce-product-category-page/
    + add fields with hooks to frontent page
    http://docs.woothemes.com/document/hooks/

  • Bernard Agustino Widjanarko

    Hi, I’m having trouble saving the input data if I use the checkbox, if from unchecked to checke, it can save, but when from checked to unchecked, it does not save, is there any way to resolve this? Thanks.

  • Santtu Jaaksola

    Here’s how you do it:

    you just have to change “add_action” lines, this uses woocommerce product category taxonomy (product_cat)

    Add new meta field function
    add_action( ‘product_cat_add_form_fields’, ‘xxx_taxonomy_add_new_meta_field’, 10, 2 );

    Edit meta field function
    add_action( ‘product_cat_edit_form_fields’, ‘xxx_taxonomy_edit_meta_field’, 10, 2 );

    Save custom meta function
    add_action( ‘edited_product_cat’, ‘save_taxonomy_custom_meta’, 10, 2 );
    add_action( ‘create_product_cat’, ‘save_taxonomy_custom_meta’, 10, 2 );

  • http://www.mente-encendida.com/ Jose Lazo

    Thanks you very much!

    I have done much the tutorial. I have successfully added an extra product categories in WooCommerce field.

    Thank You