Using Global Product Images in WooCommerce Multisite

In this tutorial we’re going to talk about how to make WooCommerce display images from the main store (and we decide which store is going to be the main). The next question that you may have is how did that happen that you have the same products across different stores of your multisite network. And the reasons can be pretty much different, for example:

And the whole idea of this tutorial is to prevent the product images from being duplicated to every sub-store uploads folders like /uploads/sites/2, /uploads/sites/3 and so on.

1. Prevent the Product Image and Gallery Images from Being Copied to Sub-stores

As you might’ve guessed we’re going to talk only about the main product image and gallery images, because these ones are stored in the WordPress database in wp_postmeta table as attachment IDs. If you have any images in a product description like <img src="" />, there is nothing to worry about, we can easily use the original URLs here.

I am not sure which way you’re using to create product copies on sub-sites but you need to make sure the following:

In case of my multisite crossposting plugin you just need to tell the plugin to not copy images to sub-stores, it can be done easily with the rudr_pre_crosspost_ignore_images hook.

add_filter( 'rudr_pre_crosspost_ignore_images', '__return_true' );

2. Get Product Images from the Original Store

How are we going to do that?

  1. There is a specific reason why I asked you to preserve attachment IDs in the product meta – because we’re going to switch to the main store using switch_to_blog() function and then we’re going to use wp_get_attachment_image_src() to get the image information.
  2. I didn’t find a convenient WooCommerce hook where you can easily do the replacement, there is actually the woocommerce_product_get_image hook, but it doesn’t work for gallery images for example, so I decided to stick to wp_get_attachment_image_src, the only thing we need to remember is that we’re only filtering images for products, so we need to add a post type condition.

Below is the code I took from my another tutorial about the multisite shared media library and I slightly modified it for our goal:

add_filter( 'wp_get_attachment_image_src', 'rudr_get_network_product_image_by_id', 10, 4 );

function rudr_get_network_product_image_by_id( $image, $attachment_id, $size, $icon ) {

	// the easiest way to check whether we're displaying an image for a product
	global $post;
	if( 'product' !== $post->post_type ) {
		return $image;
	}

	// the second way seems a little bit slower to me, but at the same time more accurate
	// $post_id = wp_get_post_parent_id( $attachment_id );
	// if( ! $post_id || 'product' !== get_post_type( $post_id ) ) {
	// 	return $image;
	// }

	// that's the main store ID where we're going to get images from, you need to proide your own value here
	$main_store_id = 1;
	// do nothing if we're already on the main store
	if( $main_store_id === get_current_blog_id() ) {
		return $image;
	}

	// get the image
	// remove_filter( 'wp_get_attachment_image_src', __FUNCTION__, 10, 4 );
	switch_to_blog( $main_store_id );
	$image = wp_get_attachment_image_src( $attachment_id, $size, $icon );
	// add_filter( 'wp_get_attachment_image_src', __FUNCTION__, 10, 4 );
	restore_current_blog();

	return $image;

}

You can see remove_filter() and add_filter() lines here, I added them because for the first look it seems like this function is going to run in an endless loop and trigger “PHP Fatal error: Allowed memory size exhausted”, but then I commented these lines because we have a condition on line 20-22 which prevents that from happening anyway.

Currently I am not sure about including this functionality into my plugin, but what do you think guys, is this option would be cool to have? Let me know in the comments below.

Something like this:

global images settings in WooCommerce multisite
Misha Rudrastyh

Misha Rudrastyh

Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.

Need some developer help? Contact me

Follow me on X