Skip to content

Instantly share code, notes, and snippets.

@liranop
Last active February 25, 2024 21:40
Show Gist options
  • Save liranop/2f32daff41192906974a624097747ace to your computer and use it in GitHub Desktop.
Save liranop/2f32daff41192906974a624097747ace to your computer and use it in GitHub Desktop.
//
// 1. Change the field name in line 20 instead of 'thumbnail_id'.
// 2. Change the taxonomy slug in line 30 instead of 'product_tag'.
//
// Display thumbnail in admin column as the second column
function display_custom_tag_thumbnail( $columns ) {
// Insert 'tag-thumbnail' as the second column
$columns_before = array_slice( $columns, 0, 1 ); // Extract the first column
$columns_after = array_slice( $columns, 1 ); // Extract the remaining columns
// Combine columns with 'tag-thumbnail' in the middle
$columns = array_merge( $columns_before, array( 'tag-thumbnail' => __( 'Thumbnail', 'text-domain' ) ), $columns_after );
return $columns;
}
// Use a priority of 5 to ensure it runs early
add_filter( 'manage_edit-product_tag_columns', 'display_custom_tag_thumbnail', 5 );
function display_custom_tag_thumbnail_value( $content, $column_name, $term_id ) {
if ( 'tag-thumbnail' === $column_name ) {
$thumbnail_id = get_term_meta( $term_id, 'thumbnail_id', true ); // REPLACE 'thumbnail_id' with yours field name
if ( $thumbnail_id ) {
$image_url = wp_get_attachment_image_url( $thumbnail_id, 'thumbnail' );
if ( $image_url ) {
$content = '<img src="' . esc_url( $image_url ) . '" alt="" style="max-width:50px;height:auto;" />';
}
}
}
return $content;
}
add_filter( 'manage_product_tag_custom_column', 'display_custom_tag_thumbnail_value', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment