Creating Taxonomy Custom Fields in WordPress : Code Examples

A. Overview of taxonomies in WordPress

Taxonomies in WordPress are a way of classifying and organizing content. By default, WordPress provides built-in taxonomies like categories and tags, which are used to categorize posts. However, custom taxonomies offer the flexibility to create additional classification systems for various post types, such as products, events, or portfolio items. Custom taxonomies enable you to create specific terms and organize your content in a way that suits your website’s unique needs.

B. Definition of custom fields in WordPress

In WordPress, custom fields are a feature that allows you to add extra metadata or information to posts, pages, or other content types. They provide a way to extend the default set of fields and store additional data relevant to your content. Custom fields are commonly used to add specific details, such as author information, publication date, ratings, or any other custom information that is not part of the default content structure.

1. Adding a Custom Field to a Taxonomy

A. Understanding the Need for a Custom Field

When working with taxonomies in WordPress, you may find that the default fields provided by the system (such as name, slug, and description) are not sufficient to capture all the relevant information for your taxonomy terms. This is where custom fields come into play. A custom field allows you to add additional data or metadata to your taxonomy terms, enabling you to store and retrieve specific information that is relevant to your site’s content.

B. Displaying the custom field on the taxonomy add page

To display the custom field on the taxonomy add page, I use category_add_form_fields action hook, because i want to add the field to category taxonomy, so replace it with your taxonomy {your-taxonomy-slug}__add_form_fields. Here’s an example:

<?php

function add_taxonomy_custom_field() {

    ?>
        <div class="form-field">
            <label for="term_meta_author"><?php _e( 'Author', 'text-domain' ); ?></label>
            <input type="text" name="term_meta[author]" id="term_meta_author" value="">
            <p class="description"><?php _e( 'Enter the author name for this category.', 'text-domain' ); ?></p>
        </div>
    <?php

}
add_action( 'category_add_form_fields', 'add_taxonomy_custom_field' );

C. Displaying the custom field on the taxonomy edit page

The same thing here, replace category with your taxonomy {your-taxonomy-slug}__edit_form_fields.

<?php

function edit_taxonomy_custom_field( $term ) {

    $term_id = $term->term_id;
    $term_meta = get_option( "taxonomy_$term_id" );
    ?>
        <tr class="form-field">
            <th scope="row" valign="top">
                <label for="term_meta_author"><?php _e( 'Author', 'text-domain' ); ?></label>
            </th>
            <td>
                <input type="text" name="term_meta[author]" id="term_meta_author" value="<?php echo esc_attr( $term_meta['author'] ?? '' ); ?>">
                <p class="description"><?php _e( 'Enter the author name for this category.', 'text-domain' ); ?></p>
            </td>
        </tr>
    <?php
}
add_action( 'category_edit_form_fields', 'edit_taxonomy_custom_field' );

D. Saving the custom field

To properly save a custom field, you should utilize two action hooks: edited_category and create_category. Remember to replace category with the appropriate taxonomy slug.

function save_taxonomy_custom_field( $term_id ) {

    if (isset($_POST['term_meta'])) {
        $term_meta = get_option("taxonomy_" . $term_id);
        $cat_keys = array_keys($_POST['term_meta']);
        
        foreach ($cat_keys as $key) {
            if (isset($_POST['term_meta'][$key])) {
                $term_meta[$key] = wp_unslash( $_POST['term_meta'][$key] );
            }
        }
        // Save the option array.
        update_option("taxonomy_" . $term_id, $term_meta);
    }
}
add_action('edited_category', 'save_taxonomy_custom_field');
add_action('create_category', 'save_taxonomy_custom_field');
#