Adding Custom Meta Box to Taxonomies
There are basically three main functions which will be used to add custom meta box for taxonomy and these functions are invoked through following hooks.
1. {taxonomy_name}_add_form_fields
2. {taxonomy_name}_edit_form_fields
3. edited_{taxonomy_name}
4. create_{taxonomy_name}
Here, we can replace taxonomy_name with any default or custom created taxonomy accordingly.I have used “Woocommerce product taxonomy” in below mentioned example code and created a plugin for same.Please check the following functions to add custom meta box:
{taxonomy_name}_add_form_fields add new custom field to add new term page form.Here I am creating a field to add a Class for term.
1 2 3 4 5 6 7 8 9 10 |
public function mj_taxonomy_add_custom_meta_field() { ?> <div class="form-field"> <label for="term_meta[class_term_meta]"><?php _e( 'Add Class', 'MJ' ); ?></label> <input type="text" name="term_meta[class_term_meta]" id="term_meta[class_term_meta]" value=""> <p class="description"><?php _e( 'Enter a value for this field','MJ' ); ?></p> </div> <?php } add_action( 'product_cat_add_form_fields', 'mj_taxonomy_add_custom_field', 10, 2 ); |
{taxonomy_name}_edit_form_fields add a custom field on taxonomy term edit page
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public function mj_taxonomy_edit_custom_meta_field($term) { $t_id = $term->term_id; $term_meta = get_option( "taxonomy_$t_id" ); ?> <tr class="form-field"> <th scope="row" valign="top"><label for="term_meta[class_term_meta]"><?php _e( 'Add Class', 'MJ' ); ?></label></th> <td> <input type="text" name="term_meta[class_term_meta]" id="term_meta[class_term_meta]" value="<?php echo esc_attr( $term_meta['class_term_meta'] ) ? esc_attr( $term_meta['class_term_meta'] ) : ''; ?>"> <p class="description"><?php _e( 'Enter a value for this field','MJ' ); ?></p> </td> </tr> <?php } add_action( 'product_cat_edit_form_fields', 'mj_taxonomy_edit_custom_meta_field', 10, 2 ); |
The next and last function to save to value passed through add or edit form:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public function mj_save_taxonomy_custom_meta_field( $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]; } } // Save the option array. update_option( "taxonomy_$t_id", $term_meta ); } } add_action( 'edited_product_cat', 'mj_save_taxonomy_custom_meta_field', 10, 2 ); add_action( 'create_product_cat', 'mj_save_taxonomy_custom_meta_field', 10, 2 ); |
Hi, (and thanks!)
Any idea how to customise how the custom taxonomy appears in the post form? Currently it looks (and functions) like a tag field.
Cheers!
Actually,this post is for adding custom fields to taxonomy.Regarding ,your query,you can display taxonomy terms as list of check boxes or search input box like tags in admin panel.
Hi. Yeah, I knew that 😉 I am adding custom fields to a custom taxonomy. I manage to find out what I needed through this old post. Thanks. http://shibashake.com/wordpress-theme/wordpress-custom-taxonomy-input-panels
http://shibashake.com/wordpress-theme/wordpress-custom-taxonomy-input-panels
This post is not useful to add custom fields to a taxonomy.It is for adding custom taxonomy as an input fields like tags to a post type.here they have created a taxonomy and created a metal box to post and add taxonomy to post through this meta box.