Add Images To Category Fields ( custom Taxonomies too )
<?php
// Add term page
function equipment_cats_add_new_meta_field() {
// this will add the custom meta field to the add new term page
?>
<div class="cppo_meta_input_wrap ">
<label>Mobile Header Image</label>
<input class="ib cppo_set_image" name="cppo_set_image" type="file">
<div class="display_image"> </div>
<input class="image_src" type="text" name="term_meta[featured_image]" id="term_meta[featured_image]" value="">
</div>
<?php
}
add_action( 'equipment_cats_add_form_fields', 'equipment_cats_add_new_meta_field', 10, 2 );
// Edit term page
function equipment_cats_edit_meta_field($term) {
// put the term ID into a variable
$t_id = $term->term_id;
// retrieve the existing value(s) for this meta field. This returns an array
$term_meta = get_option( "taxonomy_$t_id" ); ?>
<div class="cppo_meta_input_wrap ">
<label>Mobile Header Image</label>
<input class="ib cppo_set_image" name="cppo_set_image" type="file">
<div class="display_image">
<? if (!empty($term_meta['featured_image'])) { ?>
<img class="image_preview" src="<?php echo esc_attr( $term_meta['featured_image'] ) ? esc_attr( $term_meta['featured_image'] ) : ''; ?>">
<? } ?>
</div>
<input class="image_src" type="text" name="term_meta[featured_image]" id="term_meta[featured_image]" value="<?php echo esc_attr( $term_meta['featured_image'] ) ? esc_attr( $term_meta['featured_image'] ) : ''; ?>">
</div>
<?php
}
add_action( 'equipment_cats_edit_form_fields', 'equipment_cats_edit_meta_field', 10, 2 );
// Save extra taxonomy fields callback function.
function save_equipment_cats_custom_meta( $term_id ) {
if ( isset( $_POST['term_meta'] ) ) {
$t_id = $term_id;
$term_meta = get_option( "taxonomy_$t_id" );
$cat_keys = array_keys( $_POST['term_meta'] );
foreach ( $cat_keys as $key ) {
if ( isset ( $_POST['term_meta'][$key] ) ) {
$term_meta[$key] = $_POST['term_meta'][$key];
}
}
// Save the option array.
update_option( "taxonomy_$t_id", $term_meta );
}
}
add_action( 'edited_equipment_cats', 'save_equipment_cats_custom_meta', 10, 2 );
add_action( 'create_equipment_cats', 'save_equipment_cats_custom_meta', 10, 2 );
/*
TO OUTPUT IT
$metafieldArray = get_option('taxonomy_'. $term->term_id);
$metafieldoutput = $metafieldArray['custom_term_meta'];
echo $metafieldoutput;
*/