6

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!

4
  • Sharing the folder may work... however I don't believe the other installs will 'pick up' anything not uploaded through their admin interface. wordpress.org/extend/plugins/add-from-server I've used this plugin in the past to grab ftp'd media, it may help you as well. Commented Sep 23, 2011 at 11:03
  • I know you said you don't run multisite but this question on SO might still help
    – Cilvic
    Commented Sep 23, 2011 at 14:11
  • 1
    I don't currently use multi-site but could consider it if it made sense. I'm on WP 3 anyway. Thanks for the pointer. I'm not sure though I should run multi-site, I'm afraid of how Google would evaluate such a site if I were to run multi-site with the domain mapping plugin. Getting penalized for being lazy is not my style ;-)
    – Lorenzo
    Commented Sep 23, 2011 at 19:48
  • This question was way ahead of it's time... he even mentions symlinks! Commented Jul 28, 2022 at 20:08

2 Answers 2

1

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.

0

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.

2
  • 1
    This does not share the media library, the media library is not a file/folder viewer, it lists posts in the database of type attachment. You can test this by FTP'ing a JPEG or PNG into the uploads folder, it won't appear in the media library. If you symlink another WP installs upload folder to an existing one that doesn't mean the new install will have the same media library and all the same media available.
    – Tom J Nowell
    Commented Jul 28, 2022 at 22:39
  • @TomJNowell This does share both the entire media library and attachment metadata along with the physical files if you are syncing/pushing database (which we do). I can edit my answer to add that extra bit of information. Commented Jul 28, 2022 at 23:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.