There are numerous plugins available that add custom meta fields to posts, pages, and the like. Equally numerous are the tutorials walking developers through how to do so. The one thing that isn’t commonly taught, however, is how to add custom meta fields to taxonomies. This oversight is in no small part due to how difficult custom meta fields for taxonomies was until the release of WordPress 4.4. In 2011, our friend Pippin released a tutorial on adding custom meta fields to taxonomies. We thought it was time to post an updated tutorial now that custom meta fields aren’t such a nightmare.

Much like Pippin’s example, adding a custom meta field to a taxonomy still requires three 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 value of the custom field from both pages.

The Add New Page

function my_taxonomy_add_meta_fields( $taxonomy ) {
    ?>
    <div class="form-field term-group">
        <label for="my_field"><?php _e( 'My Field', 'my-plugin' ); ?></label>
        <input type="text" id="my_field" name="my_field" />
    </div>
    <?php
}
add_action( 'my_taxonomy_add_form_fields', 'my_taxonomy_add_meta_fields', 10, 2 );

As you can see, this is a straightforward function — no need to deal with pre-saved values, just a simple HTML form. We should note that, while our example doesn’t demonstrate it, if you are adding more than one field it might be a good idea to make the name of the input an array. This array would allow us to iterate through the array on saving rather than having to save each value individually.

Take note of the action we are adding the function to. It takes the form {$taxonomy_name}_add_form_fields. This pattern will persist throughout the process.

The Edit Term Page

function my_taxonomy_edit_meta_fields( $term, $taxonomy ) {
    $my_field = get_term_meta( $term->term_id, 'my_field', true );
    ?>
    <tr class="form-field term-group-wrap">
        <th scope="row">
            <label for="my_field"><?php _e( 'My Field', 'my-plugin' ); ?></label>
        </th>
        <td>
            <input type="text" id="my_field" name="my_field" value="<?php echo $my_field; ?>" />
        </td>
    </tr>
    <?php
}
add_action( 'my_taxonomy_edit_form_fields', 'my_taxonomy_edit_meta_fields', 10, 2 );

Much like the Add New page, this is a simple form. The only differences are the addition of the $term parameter being passed to the function, the retrieving of the meta value, and echoing that value in the field. Also similar to the Add New page is the action hook: {$taxonomy_name}_edit_form_fields.

Saving Your Custom Meta

function my_taxonomy_save_taxonomy_meta( $term_id, $tag_id ) {
    if( isset( $_POST['my_field'] ) ) {
        update_term_meta( $term_id, 'my_field', esc_attr( $_POST['my_field'] ) );
    }
}
add_action( 'created_my_taxonomy', 'my_taxonomy_save_taxonomy_meta', 10, 2 );
add_action( 'edited_my_taxonomy', 'my_taxonomy_save_taxonomy_meta', 10, 2 );

This should look familiar; it’s the same as saving any other meta. The one thing that’s different is that unlike post meta, we have to use two separate hooks for creating and updating meta. These hooks are created_{$taxonomy_name} and edited_{$taxonomy_name}. And that’s all there is to it! (Or is it…?)

Doing More With Custom Meta

So now that we’ve created our custom meta field, what if we wanted to display that data in the taxonomy table? It’s quite simple! Doing so requires two functions: one to add the column, and one to display the data in each row.

function my_taxonomy_add_field_columns( $columns ) {
    $columns['my_field'] = __( 'My Field', 'my-plugin' );

    return $columns;
}
add_filter( 'manage_edit-my_taxonomy_columns', 'my_taxonomy_add_field_columns' );

This should be fairly self-explanatory. Simply hook the manage_edit-{$taxonomy_name}_columns filter and add your column (or columns) to the array. What you use for the index is irrelevant, but for the sake of simplicity, we’ve chosen to use the field name for the column index. Once you have a column defined, you have to populate it!

function my_taxonomy_add_field_column_contents( $content, $column_name, $term_id ) {
    switch( $column_name ) {
        case 'my_field' :
            $content = get_term_meta( $term_id, 'my_field', true );
            break;
    }

    return $content;
}
add_filter( 'manage_my_taxonomy_custom_column', 'my_taxonomy_add_field_column_contents', 10, 3 );

The manage_{$taxonomy_name}_custom_column filter takes three arguments: $content (the content for the field, $column_name (the name of the column), and $term_id (the ID of the term; i.e.: the row we are working with). We only want to edit the content for our custom field, hence the switch statement. In our example, we’ve chosen to simply output the value of the custom meta field, but you can do pretty much anything with the retrieved data.

That’s all folks! Adding custom meta fields to taxonomies is no longer a headache! If you have any questions or comments, please feel free to leave a comment below, and happy coding!

Show Full Content
Previous Editor Wars
Next What’s This?

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Newsletter


I don't like spam either.
Your email address is secure.

Featured Posts

Close
Close