How to use wp 4.4 term meta functions
Hello
Today i used for the first time the all new term meta functions of the WordPress 4.4 version(currently in RC1).
Following is a quick overview of how i implemented it.
The two hooks i used are the ones we are using for a time now.I assume that new ones are coming.
add_action( yourTaxonomyName, array( $this, 'edit_my_term_meta_fields' ), 10, 2);
add_action( yourTaxonomyName, array( $this, 'save_my_term_meta_fields' ), 10, 2);
function edit_my_term_meta_fields($tag) {
//check for existing taxonomy meta for term ID
$t_id = $tag->term_id;
$term_meta = get_term_meta( $t_id, "", false );//False return all meta
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="id"><?php _e('Catalog Position'); ?></label></th>
<td>
<input type="text" name="term_meta[position]" id="term_meta[position]" size="3" style="width:60%;" value="<?php echo $term_meta['position'][0] ? $term_meta['position'][0] : ''; ?>"><br />
<span class="description"><?php _e('Catalog Position'); ?></span>
</td>
</tr>
<tr class="form-field">
<th scope="row" valign="top"><label for="id"><?php _e('Catalog Discount'); ?></label></th>
<td>
<input type="text" name="term_meta[discount]" id="term_meta[discount]" size="3" style="width:60%;" value="<?php echo $term_meta['discount'][0] ? $term_meta['discount'][0] : ''; ?>"><br />
<span class="description"><?php _e('Catalog Discount i.e. 5% or 0.2%u20AC'); ?></span>
</td>
</tr>
<?php
}
function save_my_term_meta_fields( $term_id ) {
if ( isset( $_POST['term_meta'] ) ) {
$cat_keys = array_keys($_POST['term_meta']);
foreach ($cat_keys as $key){
if (isset($_POST['term_meta'][$key])){
update_term_meta ( $term_id, $key, $_POST['term_meta'][$key] );
}
}
}
}