Is it possible to share WordPress media files among different domains?

Have multiple Wordpress instances that live on separate domains (not a multisite installation).

Since they provide the same content in different languages I’d like to share the images among these instances. Ideally these Wordpress installations would share the same Media Library so that I have to upload the files only once and they would be available on the other site as well, even served by the same CDN and sharing the same URL.

So for example I upload Image1.jpg on site Example.com and this gets copied to the CDN by W3 Total Cache which serves the image from MyCdn.com/static/Image1.jpg

The other site Esempio.it would have the Image1 already available in the Media Library without needing to upload it and be able to serve the same image from the same URL MyCdn.com/static/Image1.jpg

Since both sites live in the same server I was thinking of using the Store uploads in this folder setting from Media Settings and have that target folder be in fact a symlink.

What’s the suggested best practice for this scenario? The space needed is not a real concern but I was more concerned by ease of maintenance.

Best Answer

The short answer is that this cannot be done with vanilla WordPress (I know, I tried). However, a little coding could make that happen.

Images are stored in WordPress as a built-in post type – Attachments. This is what you would need to update on the remote sites with details of the CDN image as the subject.

My approach (that I have considered doing for a multisite) would be this.

Write a small plugin that can add attachments programmatically when notified of new ones. I would provide a white list of sources (my sites) and if the ping came from said site, and the image was found on it, then the attachment would be made locally with the image either copied to local or remotely stored (say in your CDN).

As I have recently learned, you can create custom JSON endpoints in WordPress. These endpoints would be what you would need to have the sites “talk” to each other to share updates about new media.

If you do this, be careful to lock those endpoints down tight. If you are not careful with incoming data, sooner or later some malicious so-and-so will cause you to have a very bad day. This is why I suggest white-list only. That or some sort of signed certificate using public/private keys.

I have seen a lot of people asking for shared media in a lot of places. Whoever sat down and got something like this working would be most popular.

I hope that helps.

You may Also Like:

Leave a Comment