Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save iWebbers/bb729957507d8eaa8ae56ab82e405ede to your computer and use it in GitHub Desktop.

Select an option

Save iWebbers/bb729957507d8eaa8ae56ab82e405ede to your computer and use it in GitHub Desktop.
// Get WooCommerce product image url by ID via shortcode: [iw_product_image_url id='']
function iw_product_image_url_shortcode( $atts ) {
$atts = shortcode_atts( array(
'id' => null,
), $atts, 'bartag' );
$html = '';
if( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ){
$_product = wc_get_product( $atts['id'] );
$image = $_product->get_image();
$remove = str_replace('<img src="', '', $image);
$html = str_replace('" class="attachment-shop_thumbnail size-shop_thumbnail wp-post-image" alt="" />', '', $remove);
}
return $html;
}
add_shortcode( 'iw_product_image_url', 'iw_product_image_url_shortcode' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment