ruttoa of Web-veistamo
6/1/2017 - 12:54 PM

Add wordpress custom meta fields to categories, example from flaba project Keywords: custom meta, taxonomy, category

Add wordpress custom meta fields to categories, example from flaba project Keywords: custom meta, taxonomy, category

//Product Cat Create page
function wh_taxonomy_add_new_meta_field() {
    ?>

    <div class="form-field">
        <label for="wh_meta_maxqty"><?php _e('Maksimilippumäärä (Paikalle)', 'wh'); ?></label>
        <input type="text" name="wh_meta_maxqty" id="wh_meta_maxqty">
        <p class="description"><?php _e('Teoreettinen maksimi esityspaikan ihmismäärälle', 'wh'); ?></p>
    </div>
    <?php
}

//Product Cat Edit page
function wh_taxonomy_edit_meta_field($term) {

    //getting term ID
    $term_id = $term->term_id;

    // retrieve the existing value(s) for this meta field.
    $wh_meta_maxqty = get_term_meta($term_id, 'wh_meta_maxqty', true);
    ?>
    <tr class="form-field">
        <th scope="row" valign="top"><label for="wh_meta_maxqty"><?php _e('Maksimilippumäärä (Paikalle)', 'wh'); ?></label></th>
        <td>
            <input type="text" name="wh_meta_maxqty" id="wh_meta_maxqty" value="<?php echo esc_attr($wh_meta_maxqty) ? esc_attr($wh_meta_maxqty) : ''; ?>">
            <p class="description"><?php _e('Teoreettinen maksimi esityspaikan ihmismäärälle', 'wh'); ?></p>
        </td>
    </tr>
   
    <?php
}

add_action('product_cat_add_form_fields', 'wh_taxonomy_add_new_meta_field', 10, 1);
add_action('product_cat_edit_form_fields', 'wh_taxonomy_edit_meta_field', 10, 1);

// Save extra taxonomy fields callback function.
function wh_save_taxonomy_custom_meta($term_id) {
    $wh_meta_maxqty = filter_input(INPUT_POST, 'wh_meta_maxqty');
    update_term_meta($term_id, 'wh_meta_maxqty', $wh_meta_maxqty);
}

add_action('edited_product_cat', 'wh_save_taxonomy_custom_meta', 10, 1);
add_action('create_product_cat', 'wh_save_taxonomy_custom_meta', 10, 1);