Skip to content

Instantly share code, notes, and snippets.

@Niloys7
Last active August 2, 2026 16:53
Show Gist options
  • Select an option

  • Save Niloys7/17b88d36c1c38844a6cf2127c15dee63 to your computer and use it in GitHub Desktop.

Select an option

Save Niloys7/17b88d36c1c38844a6cf2127c15dee63 to your computer and use it in GitHub Desktop.
How to Get WooCommerce Product Gallery Images Programmatically (PHP)

How to Get WooCommerce Product Gallery Images Programmatically (PHP)

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.

Get WooCommerce Product Gallery Images

<?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' );
}
?>

What This Code Does

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

Available Image Sizes

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)

Get Only Image URLs

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 );
}

Get Gallery Images as an Array

$images = array();

foreach ( $product->get_gallery_attachment_ids() as $attachment_id ) {
    $images[] = wp_get_attachment_url( $attachment_id );
}

print_r( $images );

Notes

  • $product must be a valid WooCommerce product object.
  • Works inside WooCommerce single product templates or anywhere a product object is available.
  • You can replace echo with your own HTML or JSON output.
  • Legacy image sizes (shop_thumbnail, shop_catalog, and shop_single) may not exist in newer WooCommerce versions. Consider using modern WordPress image sizes like woocommerce_thumbnail, woocommerce_single, and woocommerce_gallery_thumbnail when applicable.

Related WooCommerce Functions

$product->get_gallery_attachment_ids();

wp_get_attachment_url();

wp_get_attachment_image();

wp_get_attachment_image_src();


πŸš€ Enjoyed This Snippet?

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! πŸŽ‰

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment