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.

  • thanks, it works perfectly

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

  • 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 );

  • Thanks you very much!

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

    Thank You

  • I have the same problem? Did you resolved it?

  • Great and useful Tutorial….

    But …. How can I add an image uplad field?

  • Nisarg Bhavsar

    Here i have used checkbox instead of text field.When i am going to update the category it does not update.And my code is

    function tutorialshares_taxonomy_add_new_meta_field() {

    ?>

    term_id;

    $term_meta = get_option( “taxonomy_$t_id” );

    ?>

    <input type="checkbox" name="term_meta[custom_term_meta_home]" id="term_meta[custom_term_meta_home]" value="1" >

    <?php
    }
    add_action( 'product_cat_edit_form_fields', 'tutorialshares_taxonomy_edit_meta_field', 10, 2 );

    function save_taxonomy_custom_meta( $term_id ) {
    if ( isset( $_POST['term_meta'] ) ) {
    $t_id = $term_id;

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

    $cat_keys = array_keys( $_POST['term_meta'] );
    foreach ( $cat_keys as $key ) {
    if ( isset ( $_POST['term_meta'][$key] ) ) {
    $term_meta[$key] = $_POST['term_meta'][$key];
    }
    }
    update_option( "taxonomy_$t_id", $term_meta );
    }
    }
    add_action( 'edited_product_cat', 'save_taxonomy_custom_meta', 10, 2 );
    add_action( 'create_product_cat', 'save_taxonomy_custom_meta', 10, 2 );

    So where i have to change the code to solve this error?

  • developerbmw

    Do you even know how hooks in WordPress work? Why did you pass “2” as the last argument to add_action here? add_action( 'category_edit_form_fields', 'tutorialshares_taxonomy_edit_meta_field', 10, 2 );

  • martin fuks

    Hi there, excellent tutorial! I have a problem though, when adding the last bit of code (save the values), the site crashes (white screen). Any idea?

  • Charlie Putt21

    my wife required a form a few weeks ago and found an online platform that has 6,000,000 forms . If others are searching for it also , here’s a http://qr.net/bVCCA

  • David Fernando Quintero

    Hi excellent tutorial! Thanks.
    I have a cuestion, i want to add two fields in the taxonomy woocommerce product category, how to do it?