Need to retrieve WooCommerce product gallery images in your theme or plugin? This guide shows how to get all gallery image URLs and image HTML for a WooCommerce product using PHP.
Whether you're building a custom product gallery, API integration, slider, or image export tool, this snippet will help you access every gallery image attached to a product.
<?php
global $product;
$attachment_ids = $product->get_gallery_attachment_ids();
foreach ( $attachment_ids as $attachment_id ) {
// Original image URL
echo wp_get_attachment_url( $attachment_id );
// WordPress image sizes
echo wp_get_attachment_image_src( $attachment_id, 'full' )[0];
echo wp_get_attachment_image_src( $attachment_id, 'medium' )[0];
echo wp_get_attachment_image_src( $attachment_id, 'thumbnail' )[0];
// WooCommerce image sizes
echo wp_get_attachment_image_src( $attachment_id, 'shop_thumbnail' )[0];
echo wp_get_attachment_image_src( $attachment_id, 'shop_catalog' )[0];
echo wp_get_attachment_image_src( $attachment_id, 'shop_single' )[0];
// Output image HTML
echo wp_get_attachment_image( $attachment_id, 'full' );
echo wp_get_attachment_image( $attachment_id, 'medium' );
echo wp_get_attachment_image( $attachment_id, 'thumbnail' );
echo wp_get_attachment_image( $attachment_id, 'shop_thumbnail' );
echo wp_get_attachment_image( $attachment_id, 'shop_catalog' );
echo wp_get_attachment_image( $attachment_id, 'shop_single' );
}
?>This snippet loops through every gallery image attached to the current WooCommerce product and lets you retrieve:
- Original image URL
- Full-size image URL
- Medium image URL
- Thumbnail image URL
- WooCommerce image sizes
- Complete HTML
<img>markup
| Size | Description |
|---|---|
| full | Original uploaded image |
| medium | WordPress medium size |
| thumbnail | WordPress thumbnail |
| shop_thumbnail | WooCommerce thumbnail (legacy) |
| shop_catalog | WooCommerce catalog image (legacy) |
| shop_single | WooCommerce single product image (legacy) |
If you only need image URLs:
$attachment_ids = $product->get_gallery_attachment_ids();
foreach ( $attachment_ids as $attachment_id ) {
echo wp_get_attachment_url( $attachment_id );
}$images = array();
foreach ( $product->get_gallery_attachment_ids() as $attachment_id ) {
$images[] = wp_get_attachment_url( $attachment_id );
}
print_r( $images );$productmust be a valid WooCommerce product object.- Works inside WooCommerce single product templates or anywhere a product object is available.
- You can replace
echowith your own HTML or JSON output. - Legacy image sizes (
shop_thumbnail,shop_catalog, andshop_single) may not exist in newer WooCommerce versions. Consider using modern WordPress image sizes likewoocommerce_thumbnail,woocommerce_single, andwoocommerce_gallery_thumbnailwhen applicable.
$product->get_gallery_attachment_ids();
wp_get_attachment_url();
wp_get_attachment_image();
wp_get_attachment_image_src();If this code saved you time, I'd really appreciate it if you could:
- β Star this Gist
- π¦ Follow me on X for more WooCommerce & WordPress development snippets
- π Share it with other developers
More practical WooCommerce snippets and tutorials are coming soon. Happy coding! π