Monday, February 3, 2020

Create Taxonomy Term Meta in Wordpress

Just like post meta data term meta is additional information stored against a taxonomy terms. A single entry in a custom taxonomy is called term. This post demonstrates how can you add term meta to your custom taxonomy. We will create a term meta "gender" for our star taxonomy.

Create Taxonomy Term Meta in Wordpress

As wordpress allows us to add our custom post types and custom taxonomies, it also provides different hooks to extend functionality as per our need. For adding custom term meta we will need to use four hooks, One for showing term meta field on add term form, Second for showing term meta field on term edit form and third and fourth one to save the term meta whenever a term is created or updated. Wordpress hooks can be used like this:
add_action( "$taxonomy_add_form_fields", "function_to_show_fields", 10, 1 );
add_action( "$taxonomy_edit_form_fields", "function_to_show_fields", 10, 2 );
add_action( "created_$taxonomy", "funtion_to_save_fields", 10, 2 );
add_action( "edited_$taxonomy", "funtion_to_save_fields", 10, 2 );
Where $taxonomy is the slug of taxonomy in this post its going to be gender. You need to change it to your taxonomy slug for which you are going create this custom term meta. Now lets break it down in sections. We will create a separate file terms-meta.php, which will hold the following hooks code for creating our term meta.

terms-meta.php

<?php
// Hook to show term field on add term form
add_action( "star_add_form_fields", "add_star_meta_fields", 10, 1 );
function add_star_meta_fields( $taxonomy ) {?>
    <div class="form-field star-metabox">
        <label class="title-label"><?php _e( "Gender", "textdomain" ); ?></label>
        <label class="field-radio"><input type="radio" name="gender" value="male" checked="checked"><?php _e( "Male", "textdomain" )?></label>
        <label class="field-radio"><input type="radio" name="gender" value="female"><?php _e( "Female", "textdomain" )?></label>
    </div>
<?php
}

// Hook to show term fields on edit taxonomy form
add_action( "star_edit_form_fields", "taxonomy_star_edit_meta_field", 10, 2 );
function taxonomy_star_edit_meta_field( $term, $taxonomy ) {
    $gender = get_term_meta( $term->term_id, "gender", true );?>
    <tr class="form-field">
        <th scope="row" valign="top"><?php _e( "Gender", "textdomain" ); ?></th>
        <td>
            <div class="star-metabox">
                <label class="field-radio">
                    <input type="radio" name="gender" value="male"
                        <?php _e($gender == "male" ? 'checked="checked"' : '', "textdomain");?> />
                        <?php _e( "Male", "textdomain" )?>
                </label>
                <label class="field-radio">
                    <input type="radio" name="gender" value="female"
                        <?php _e($gender == "female" ? 'checked="checked"' : '', "textdomain" );?> />
                        <?php _e( "Female", "textdomain" )?>
                </label>
            </div>
        </td>
    </tr>
<?php
}

// Two hooks with same function to save term meta when term is created/edited
add_action( "created_star", "edit_star_custom_meta", 10, 2 );
add_action( "edited_star", "edit_star_custom_meta", 10, 2 );
function add_edit_star_custom_meta( $term_id, $tax_term_id ) {
    if ( isset( $_POST["gender"] ) ) {
        update_term_meta( $term_id, "gender", esc_attr($_POST["gender"]) );
    }
}
?>

Now include "terms-meta.php" in our "functions.php"
<?php include( "terms-meta.php" );?>

Function used to get term meta of a single term:
get_term_meta( $term_id, "meta_key", true );
Where $term_id is the single term id , "meta_key" is the field name we saved which was "gender" used in this post, and last parameter is used for response if set to true it will return the value.