<?php
add_action ( 'category_edit_form_fields', 'mediapulse_category_edit_form_fields_callback', 10, 2 );
function mediapulse_category_edit_form_fields_callback($ttObj, $taxonomy) {

    $mediapulse_term_id = $ttObj->term_id;
    $mediapulse_image = '';
    $mediapulse_image = get_term_meta( $mediapulse_term_id, 'term_image', true );

    ?>
    <tr class="form-field term-image-wrap">
      <th scope="row"><label for="image"><?php esc_html_e('Image','mediapulse'); ?></label></th>
	<td>
        <?php if ( $mediapulse_image ): ?>
        <span id="category_custom_image">
           <img src="<?php echo esc_url($mediapulse_image); ?>" style="width: 100%"/>
        </span>
        <input 
           type="hidden" 
           id="category_custom_image_url" 
           name="category_custom_image_url">
                
        <span>
           <a href="#" 
             class="button custom-button-upload" 
             id="custom-button-upload" 
             style="display: none"><?php esc_html_e('Upload image','mediapulse'); ?></a>
           <a href="#" class="button custom-button-remove"><?php esc_html_e('Remove image','mediapulse'); ?></a>                    
        </span>
        <?php else: ?>
        <span id="category_custom_image"></span>
        <input 
            type="hidden" 
            id="category_custom_image_url" 
            name="category_custom_image_url">
        <span>
           <a href="#" 
              class="button custom-button-upload" 
              id="custom-button-upload"><?php esc_html_e('Upload image','mediapulse'); ?></a>
           <a href="#" 
              class="button custom-button-remove" 
              style="display: none"><?php esc_html_e('Remove image','mediapulse'); ?></a>
        </span>
        <?php endif; ?>
        </td>
    </tr>
        
    <?php
}

add_action( 'edit_term', 'mediapulse_edit_term_callback' );
function mediapulse_edit_term_callback($mediapulse_term_id) {
    $mediapulse_image = '';
    $mediapulse_image = get_term_meta( $mediapulse_term_id, 'term_image' );

    if ( $mediapulse_image )
    update_term_meta( 
        $mediapulse_term_id, 
        'term_image', 
        esc_url( $_POST['category_custom_image_url']) );

    else
    add_term_meta( 
        $mediapulse_term_id, 
        'term_image', 
        esc_url( $_POST['category_custom_image_url']) );

}