I 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.
Thanks!
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.
Edit: this answer assumes you are syncing attachments in the database
If so then you can use Linux symlinks to accomplish this as long as BOTH sites are on the same server or localhost system… the domains or TLDs don’t matter, only the local file structure.
https://wordpress.stackexchange.com/a/408075/152624
I suppose with all the modern cloud solutions these days, you could even use multiple cloud servers in the same datacenter but symlink a third “object storage” or internal network socket/port/drive although I haven’t taken things that far to find out and prefer using simpler configurations… but theoretically it could be done.
Perhaps I will test something complex like this and edit here later on.
But I can confirm the localhost symlink solution is working great for our users already (we share
/var/www/html/wp-content/uploads/
from the production site with the staging site, which has a symlink at/var/www/html/staging/wp-content/uploads/
).Be sure to setup a cron job to prevent the symlink from getting overwritten during WordPress updates or other potential user errors, etc.