Home / WP Custom fields on category

WP Custom fields on category


// Show the custom fields  on add taxnomony
function smm_category_sk_add_image( $term ) {
	?>
	<div class="form-field">
		<label for="item_icon">Icon (<a href="https://fontawesome.com/v4.7.0/icons/" target="_blank">fontawesome</a>)</label>
		<input type="text" name="item_icon" id="item_icon" value="">
	</div>
	<div class="form-field">
		<label for="taxImage">Color</label>
		<input type="text" name="item_color" id="item_color" value="">
	</div>
	<div class="form-field">
		<label for="taxImage">Order</label>
		<input type="text" name="item_order" id="item_color" value="0">
	</div>
	<?php
}
add_action( 'smm_category_add_form_fields', 'smm_category_sk_add_image', 10, 2 );

// Show the custom fields  on edit taxnomony
function smm_category_sk_edit_image( $term ) {
	
	// put the term ID into a variable
	$t_id = $term->term_id;

	$item_color = get_term_meta( $t_id, 'item_color', true ); 
	$item_icon  = get_term_meta( $t_id, 'item_icon', true ); 
	$item_order = get_term_meta( $t_id, 'item_order', true );
	$item_order = ($item_order)?$item_order:'1';
	?>

	<tr class="form-field">
		<th>
			<label for="item_icon">Icon (<a href="https://fontawesome.com/v4.7.0/icons/" target="_blank">fontawesome</a>)</label>
		</th>
		<td>	 
			<input type="text" name="item_icon" id="item_icon" value="<?php echo $item_icon; ?>">
		</td>
	</tr>

	<tr class="form-field">
		<th>
			<label for="item_color">Color</label>
		</th>
		<td>	 
			<input type="text" name="item_color" id="item_color" value="<?php echo $item_color; ?>">
		</td>
	</tr>

	<tr class="form-field">
		<th>
			<label for="item_order">Order</label>
		</th>
		<td>	 
			<input type="text" name="item_order" id="item_order" value="<?php echo $item_order; ?>">
		</td>
	</tr>
	<?php
}
add_action( 'smm_category_edit_form_fields', 'smm_category_sk_edit_image', 10 );

// Save the custom fields 
function smm_category_sk_save_image( $term_id ) {

	if ( isset( $_POST['item_color'] ) ) {
		if( $item_color = $_POST['item_color'] ) {
			update_term_meta( $term_id, 'item_color', $item_color );
		}
		if( $item_icon = $_POST['item_icon'] ) {
			update_term_meta( $term_id, 'item_icon', $item_icon );
		}
		if( $item_order = $_POST['item_order'] ) {
			update_term_meta( $term_id, 'item_order', $item_order );
		}
	} 

}
add_action( 'edited_smm_category', 'smm_category_sk_save_image' );  
add_action( 'create_smm_category', 'smm_category_sk_save_image' );

all rights reserved