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.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php // Add term page function tutorialshares_taxonomy_add_new_meta_field() { // this will add the custom meta field to the add new term page ?> <div class="form-field"> <label for="term_meta[custom_term_meta]"><?php _e( 'Example meta field', 'tutorialshares' ); ?></label> <input type="text" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value=""> <p class="description"><?php _e( 'Enter a value for this field','tutorialshares' ); ?></p> </div> <?php } add_action( 'category_add_form_fields', 'tutorialshares_taxonomy_add_new_meta_field', 10, 2 ); |
Note the add_action()
parameters.
1 |
add_action( 'category_add_form_fields', 'tutorialshares_taxonomy_add_new_meta_field', 10, 2 ); |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php // Edit term page function tutorialshares_taxonomy_edit_meta_field($term) { // put the term ID into a variable $t_id = $term->term_id; // retrieve the existing value(s) for this meta field. This returns an array $term_meta = get_option( "taxonomy_$t_id" ); ?> <tr class="form-field"> <th scope="row" valign="top"><label for="term_meta[custom_term_meta]"><?php _e( 'Example meta field', 'tutorialshares' ); ?></label></th> <td> <input type="text" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="<?php echo esc_attr( $term_meta['custom_term_meta'] ) ? esc_attr( $term_meta['custom_term_meta'] ) : ''; ?>"> <p class="description"><?php _e( 'Enter a value for this field','tutorialshares' ); ?></p> </td> </tr> <?php } add_action( 'category_edit_form_fields', 'tutorialshares_taxonomy_edit_meta_field', 10, 2 ); |
Finally, we need to add the last function, which saves all the data we have inputted into the fields.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// Save extra taxonomy fields callback function. 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]; } } // Save the option array. update_option( "taxonomy_$t_id", $term_meta ); } } add_action( 'edited_category', 'save_taxonomy_custom_meta', 10, 2 ); add_action( 'create_category', 'save_taxonomy_custom_meta', 10, 2 ); |
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:
1 2 3 4 |
$metafieldArray = get_option('taxonomy_'. $term->term_id); $metafieldoutput = $metafieldArray['custom_term_meta']; echo $metafieldoutput; |