Add a custom meta field to a category in WordPress

So I was in the process of building a new plugin and needed a way to add a new field to all Woocommerce categories, this method will do that and it works with standard WordPress categories as well. Yes, I do know that this can be accomplished using a plugin but in some cases, especially when you’re writing a plugin, you don’t really want to base the functionality of your plugin on the existence of another plugin.

This code is loosely based on PIPPINS method of adding a new category field using another method where the value was added in as a new option. I have altered the code to use WordPress’s own get_term_meta, add_term_meta and update_term_meta which will fetch, store and update to the correct places.

So first things first, we need to make sure that when a new category is added that the user can populate the new field, so we’re going to add the new field onto the “Add new category” form. In your themes functions.php file (or in your plugin) add in the below function.

//Add new field to add new product category page
function pos_new_cat_meta_field() {
    //this will add the custom meta field to the add new term page
?>
<div class="form-field">
    <label for="pos_cat_id"><?php _e( 'POS Category ID', '' ); ?></label>
    <input type="text" name="pos_cat_id" id="pos_cat_id" value="">
    <?php _e( 'Assign the relevant POS category ID', '' ); ?>
</div>
<?php
}
add_action( 'product_cat_add_form_fields', 'pos_new_cat_meta_field', 10, 2 );

Keep in mind that the first parameter in add_action determines to which taxonomy the field is added to. In my case I’m adding it in for Woocommerce product categories so the taxonomy name i’m targeting is “product_cat”, if you wanted to add a new field to a standard post category it would just be “category”. So you would follow this structure: add_action( ‘{taxonomy_name}_add_form_fields’, ‘pos_new_cat_meta_field’, 10, 2 );

Next we need to add in a field for existing categories:

// Add new field to existing category edit page
function pos_edit_cat_meta_field($term) {
    // retrieve the existing value(s) for this meta field. This returns an array
    $term_meta = get_term_meta($term->term_id);
?>
<tr class="form-field">
    <th scope="row" valign="top">
        <label for="pos_cat_id"><?php _e( 'POS Category ID', '' ); ?></label>
    </th>
    <td>
        <input type="text" name="pos_cat_id" id="pos_cat_id" value="<?php echo esc_attr( $term_meta['pos_cat_id'][0] ) ? esc_attr( $term_meta['pos_cat_id'][0] ) : ''; ?>">
        <?php _e( 'Assign the relevant POS category ID','' ); ?>
    </td>
</tr>
<?php
}
add_action( 'product_cat_edit_form_fields', 'pos_edit_cat_meta_field', 10, 2 );

Again the first parameter is used to target the specific taxonomy you are working with.

Lastly, we need to apply the values every time a new taxonomy is created or an existing one is saved.

// Save additional field data callback function.
function save_pos_cat_custom_meta( $term_id ) {
    if ( isset( $_POST['pos_cat_id'] ) ) {
        $term_meta = get_term_meta($term_id);
        // Save the taxonomy value.
        update_term_meta($term_id, 'pos_cat_id', $_POST['pos_cat_id']);
    }
} 
add_action( 'edited_product_cat', 'save_pos_cat_custom_meta', 10, 2 ); 
add_action( 'create_product_cat', 'save_pos_cat_custom_meta', 10, 2 );

Again, the first parameter is used to target which taxonomy to save the value to. Test to ensure everything works and Bob’s your uncle.

Hope this helps, Nathaniel.

Other Posts

%d bloggers like this: