Is there any hook to change the single product thumbnail? I did search a lot on SO as well as over the internet but no luck.
With 'thumbnail' I don't mean changing the size of the current image but I want to completely change/replace the product image (thumbnail) with a new image based on some scenario.
public function pn_change_product_image_link( $html, $post_id ){
$url = get_post_meta( $post_id );
$alt = get_post_field( 'post_title', $post_id ) . ' ' . __( 'thumbnail', 'txtdomain' );
$attr = array( 'alt' => $alt );
$attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, NULL );
$attr = array_map( 'esc_attr', $attr );
$html = sprintf( '<img src=".svg/2000px-WP_Suspension_logo.svg.png"', esc_url($url) );
foreach ( $attr as $name => $value ) {
$html .= " $name=" . '"' . $value . '"';
}
$html .= ' />';
return $html;
}
This is what I'm doing now but it's throwing an error.
Filter, Hook:
post_thumbnail_html
Is there any hook to change the single product thumbnail? I did search a lot on SO as well as over the internet but no luck.
With 'thumbnail' I don't mean changing the size of the current image but I want to completely change/replace the product image (thumbnail) with a new image based on some scenario.
public function pn_change_product_image_link( $html, $post_id ){
$url = get_post_meta( $post_id );
$alt = get_post_field( 'post_title', $post_id ) . ' ' . __( 'thumbnail', 'txtdomain' );
$attr = array( 'alt' => $alt );
$attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, NULL );
$attr = array_map( 'esc_attr', $attr );
$html = sprintf( '<img src="https://upload.wikimedia/wikipedia/commons/thumb/3/38/WP_Suspension_logo.svg/2000px-WP_Suspension_logo.svg.png"', esc_url($url) );
foreach ( $attr as $name => $value ) {
$html .= " $name=" . '"' . $value . '"';
}
$html .= ' />';
return $html;
}
This is what I'm doing now but it's throwing an error.
Filter, Hook:
post_thumbnail_html
Share
Improve this question
asked Feb 15, 2018 at 9:12
Akash AgrawalAkash Agrawal
991 silver badge3 bronze badges
2 Answers
Reset to default 0Here's what I believe is an updated version of one of the other answers' comments. This function replaces the width and height attributes, but OP wanted to change the src. I imagine the solution would be similar, except using preg_replace to replace the src instead.
add_action('woocommerce_single_product_image_thumbnail_html','remove_single_product_image_attrs',10,2);
function remove_single_product_image_attrs( $sprintf, $post_id ){
return preg_replace( '/(width|height)="\d*"\s/', "", $sprintf );
}
I hope this filter will help you.
apply_filters( 'woocommerce_single_product_image_thumbnail_html', $sprintf, $post_id );
Ref: http://hookr.io/filters/woocommerce_single_product_image_thumbnail_html/