WooCommerce product images from URLs: sideload, gallery and REST API
Product data from a supplier usually comes with image URLs, not files. The WooCommerce REST API can take those URLs and download the images itself, and in PHP the same is done with media_sideload_image() plus set_image_id() and set_gallery_image_ids(). The parts that are easy to get wrong are duplicates (every import downloads the same image again), slow imports and images without alt text.
Problem: A nightly import creates and updates 2,000 products from a supplier feed with image URLs. After a month, the media library has 60,000 copies of the same images, the import takes hours, and none of the images has alt text.
Solution: Remember the source URL of every sideloaded image in attachment meta and reuse the existing attachment if the URL was imported before, sideload only new URLs with media_sideload_image( $url, $product_id, $alt, 'id' ), set the featured image and the gallery through the product object, and set the alt text from the product name.
Sideload once, reuse afterwards
require_once ABSPATH . 'wp-admin/includes/media.php';
require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/image.php';
function ha_attachment_from_url( string $url, int $product_id, string $alt ): int {
$existing = get_posts( array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'meta_key' => '_ha_source_url',
'meta_value' => $url,
'fields' => 'ids',
'posts_per_page' => 1,
) );
if ( $existing ) {
return (int) $existing[0];
}
$id = media_sideload_image( $url, $product_id, $alt, 'id' );
if ( is_wp_error( $id ) ) {
error_log( 'Image import failed for ' . $url . ': ' . $id->get_error_message() );
return 0;
}
update_post_meta( $id, '_ha_source_url', esc_url_raw( $url ) );
update_post_meta( $id, '_wp_attachment_image_alt', $alt );
return (int) $id;
}
media_sideload_image() downloads the file to a temporary location, checks the type, moves it into uploads, creates the attachment and generates the image sizes. With 'id' as the last argument it returns the attachment ID instead of an <img> tag. The three require_once lines are needed outside the admin (cron, WP-CLI, REST). The third argument becomes the attachment’s title; the alt text is a separate meta field. The lookup by _ha_source_url makes the import idempotent: the second night, nothing is downloaded.
Assign images to the product
function ha_set_product_images( WC_Product $product, array $urls ): void {
$alt = $product->get_name();
$ids = array_values( array_filter( array_map(
fn( $url ) => ha_attachment_from_url( $url, $product->get_id(), $alt ),
$urls
) ) );
if ( ! $ids ) {
return;
}
$product->set_image_id( array_shift( $ids ) );
$product->set_gallery_image_ids( $ids );
$product->save();
}
The first image becomes the featured image, the rest the gallery. Setting both through the product object (instead of set_post_thumbnail() and the _product_image_gallery meta) keeps WooCommerce’s caches in sync. How the gallery is shown in a template: display a WooCommerce product gallery with PHP.
The same through the REST API
curl -s -u ck_xxx:cs_xxx -X PUT -H "Content-Type: application/json" \
https://shop.example.com/wp-json/wc/v3/products/123 \
-d '{"images":[{"src":"https://supplier.example/img/123-front.jpg","alt":"Blue mug, front"},{"id":4567}]}'
An image with src is sideloaded by WooCommerce; an image with id reuses an existing attachment. The first entry is the featured image. The images array replaces the product’s images completely, and every src is downloaded again on every request, so an external import should send id for images it uploaded before.
Speed and limits
Sideloading generates all registered image sizes for every image, which is the slow part. Run imports from WP-CLI or Action Scheduler in batches, not in a web request with its time limit. Remote downloads use download_url() with a 300-second timeout by default; slow supplier servers are a common cause of failed images, which the error log above records.
NOTE: Only sideload from sources you trust. The file type is checked, but a feed that suddenly delivers 50 MB images fills the disk quickly; a size check with a HEAD request (wp_remote_head()) before downloading protects against that. Featured images in general: set the featured image programmatically.