Adding Custom Meta Fields to Taxonomies

It is pretty common knowledge that custom meta fields can be added to posts, pages and custom post types, but did you know that you can add custom meta fields to post tags, categories, and custom taxonomies as well? It’s actually pretty simple, though relatively undocumented. So in this quick tutorial I’m going to walk you through the process of added some custom meta fields to taxonomy.
Adding custom meta fields to a taxonomy requires three separate functions: one to add the field to the Add New page; one to add the field to the Edit Term page; and one to save the values of the custom field from both pages. This first function will add a custom field to the Add New page for the default Category taxonomy.

<?php 
function custom_taxonomy_add_new_meta_field() { 
?>	
<div class="form-field">
		<label for="term_meta[custom_term_meta]"><?php _e( 'Example meta field', 'cmsaddons' ); ?></label>
		<input type="text" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="">
		

<?php _e( 'Enter a value for this field','cmsaddons' ); ?>

	</div>

<?php
}
add_action( 'category_add_form_fields', 'custom_taxonomy_add_new_meta_field', 10, 2 );

Also take note of the add_action():

add_action( 'category_add_form_fields', 'custom_taxonomy_add_new_meta_field', 10, 2 );

The first parameter is what determines the taxonomy that this field gets added to. It uses this format: {$taxonomy_name}_add_form_fields. So if you wanted to add the field to your “genres” taxonomy, you would use:

add_action( 'genres_add_form_fields', 'custom_taxonomy_add_new_meta_field', 10, 2 );

Woocommerce Category:

add_action( 'product_cat_add_form_fields', 'custom_taxonomy_add_new_meta_field', 10, 2 );

You will now have a field this on the Add New term page

The second function we need is the one that adds the HTML to our taxonomy Edit Term page. It looks very similar, though the HTML layout is a little different, and we also have to do a check to see if the meta field has any data saved already, so that we can populate the field on load.

<?php // Edit term page 
function custom_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', 'cmsaddons' ); ?></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'] ) : ''; ?>">
			

<?php _e( 'Enter a value for this field','cmsaddons' ); ?>

		</td>

	</tr>

<?php
}
add_action( 'category_edit_form_fields', 'custom_taxonomy_edit_meta_field', 10, 2 );

First, we have passed the $term parameter to our function. This variable will contain the database object for our term. The only information we need for our function is the term ID, so we put that into a variable to make things easy. We then use the get_option() function to retrieve the value of out term meta. Note, all term meta is stored in an array in the option named “taxonomy_{$term_id}”. So to get the value of our specific meta field, we’d use $term_meta[‘custom_term_meta’];

Next is our save function. This will take the value entered in the field and store it in the taxonomy option.

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

The function takes the ID of our term as a parameter. A quick check if performed to make sure that data is being sent from our custom field, and if it is, the current value of the field is retrieved from the database. Next, each of our custom fields (remember, we can setup more than one in a single array) is looped through. In the loop, each of the field values is inserted into the $term_meta array. Once the loop is complete, the taxonomy option is updated, storing our field values in the database.

Get Meta Fields:

$queried_object = get_queried_object();
if (isset($queried_object->term_id)){
  $term_id = $queried_object->term_id;  
  $cmsaddons_term_meta = get_option( "taxonomy_".$term_id,'' );
  echo $cmsaddons_term_meta['custom_term_meta']	;
}

That’s it! The process is really pretty simple.

Example

// Add term page
function custom_url_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[cat_select_temp]"><?php _e( 'Select Template category', 'cmsaddons' ); ?></label>
		<select name="term_meta[cat_select_temp]">
<option value="childcat"><?php esc_html_e('Sub category','cmsaddons'); ?></option>
<option value="tutcat"><?php esc_html_e('Tutorial category','cmsaddons'); ?> </option>
		  
		</select>
		
	<label for="term_meta[custom_term_meta]"><?php _e( 'Tutorial', 'cmsaddons' ); ?></label>
	<?php 
            wp_editor(wp_kses_post($content , ENT_QUOTES, 'UTF-8'), 'term_meta[cat_tutorial]');
        ?>
		

<?php _e( 'Inserisci un custom url prodotto per la categoria','cmsaddons' ); ?>

	</div>

<?php } 
add_action( 'product_cat_add_form_fields', 'custom_url_taxonomy_add_new_meta_field', 10, 2 );
function custom_url_taxonomy_edit_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[cat_select_temp]"><?php _e( 'Select Template category', 'cmsaddons' ); ?></label></th>		
<td>			
		<?php $cat_select_temp = esc_attr( $term_meta['cat_select_temp'] ) ? esc_attr( $term_meta['cat_select_temp'] ) : '';?>
		<select id="term_meta[cat_select_temp]" name="term_meta[cat_select_temp]" class="postform">
<option value="childcat" <?php selected( 'childcat', $cat_select_temp ); ?>><?php _e( 'Sub category', 'cmsaddons' ); ?></option>
<option value="tutcat" <?php selected( 'tutcat', $cat_select_temp ); ?>><?php _e( 'Tutorial category', 'cmsaddons' ); ?></option>
		</select>	
		</td>
	</tr>	
	
<tr class="form-field">
		
<th scope="row" valign="top"><label for="term_meta[cat_tutorial]"><?php _e( 'Tutorial', 'cmsaddons' ); ?></label></th>		
<td>
<?php $content = esc_attr( $term_meta['cat_tutorial'] ) ? esc_attr( $term_meta['cat_tutorial'] ) : ''; wp_editor(wp_kses_post($content , ENT_QUOTES, 'UTF-8'), 'term_meta[cat_tutorial]'); ?>
		</td>

		</tr>

<?php
}
add_action( 'product_cat_edit_form_fields', 'custom_url_taxonomy_edit_meta_field', 10, 2 );
// 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_product_cat', 'save_taxonomy_custom_meta', 10, 2 );  
add_action( 'create_product_cat', 'save_taxonomy_custom_meta', 10, 2 );