function custom_add_attribute_thumbnail_field( $term ) {
$thumbnail_id = get_term_meta( $term->term_id, '_thumbnail_id', true );
$image_url = wp_get_attachment_image_url( $thumbnail_id, 'thumbnail' ); // Imposta la dimensione dell'immagine desiderata (esempio: 'thumbnail', 'medium', 'large')
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="attribute_thumbnail"><?php esc_html_e( 'Attribute Thumbnail', 'text-domain' ); ?></label></th>
<td>
<div id="attribute_thumbnail_preview">
<?php if ( $image_url ) : ?>
<img src="<?php echo esc_url( $image_url ); ?>" alt="<?php echo esc_attr( $term->name ); ?>" style="max-width:100px; max-height:100px;">
<?php endif; ?>
</div>
<input type="hidden" id="attribute_thumbnail" name="attribute_thumbnail" value="<?php echo esc_attr( $thumbnail_id ); ?>">
<button type="button" id="upload_attribute_thumbnail_button" class="button"><?php esc_html_e( 'Upload/Add image', 'text-domain' ); ?></button>
<button type="button" id="remove_attribute_thumbnail_button" class="button"><?php esc_html_e( 'Remove image', 'text-domain' ); ?></button>
<script>
jQuery(document).ready(function($) {
// Carica l'immagine
$('#upload_attribute_thumbnail_button').click(function() {
var custom_uploader = wp.media({
title: '<?php esc_html_e( "Choose or Upload Image", "text-domain" ); ?>',
button: {
text: '<?php esc_html_e( "Use Image", "text-domain" ); ?>'
},
multiple: false
}).on('select', function() {
var attachment = custom_uploader.state().get('selection').first().toJSON();
$('#attribute_thumbnail').val(attachment.id);
$('#attribute_thumbnail_preview').html('<img src="' + attachment.url + '" alt="<?php echo esc_attr( $term->name ); ?>" style="max-width:100px; max-height:100px;">');
}).open();
});
// Rimuovi l'immagine
$('#remove_attribute_thumbnail_button').click(function() {
$('#attribute_thumbnail').val('');
$('#attribute_thumbnail_preview').html('');
});
});
</script>
<p class="description"><?php esc_html_e( 'Upload or select an image to set as the attribute thumbnail.', 'text-domain' ); ?></p>
</td>
</tr>
<?php
}
add_action( 'pa_attributo_add_form_fields', 'custom_add_attribute_thumbnail_field', 10, 2 );
add_action( 'pa_attributo_edit_form_fields', 'custom_add_attribute_thumbnail_field', 10, 2 );
function custom_save_attribute_thumbnail( $term_id ) {
if ( isset( $_POST['attribute_thumbnail'] ) ) {
$thumbnail_id = absint( $_POST['attribute_thumbnail'] );
update_term_meta( $term_id, '_thumbnail_id', $thumbnail_id );
}
}
add_action( 'edited_pa_attributo', 'custom_save_attribute_thumbnail', 10, 2 );