Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm currently looking to modify Wordpress so that it always uses the primary site for images. I am achieving this using:

function use_main_blog_library()
{
    echo "<script>alert('is happenin');</script>";
    switch_to_blog(1);
}
add_action('load-media-new.php', 'use_main_blog_library');
add_action('load-media-upload.php', 'use_main_blog_library');
add_action('load-media.php', 'use_main_blog_library');
add_action('load-upload.php', 'use_main_blog_library');

Whilst this works for the old media library, it does not change the new media library. Is there any way I can set the new one (which I know is heavily reliant on JavaScript) to also use the primary library?

share|improve this question
add comment

1 Answer 1

up vote 6 down vote accepted

Unless you use the primary site to host all of your content, including pages and posts, this isn't going to be possible, and the nearest you can get requires a lot of kludges.

Your existing 'kludge' invokes a lot of serious problems, and was a ticking timebomb of maintenance performance and security issues. This would be the first major issue, and I can guarantee more will come.

But that doesn't mean your original problem is invalid, just that your fix isn't the solution, and a fix for a fix, is never good.

Why is it bad?

The problem lies in how media is stored. Yes the files are stored on the server disk, but WordPress doesn't store its data in the files, it stores them as attachments.

Attachments are posts, with post type 'attachment', with post meta, comments, taxonomies, titles, post status, etc. Since each site in a network has its own posts table, and since attachments have their parent set as the post they're attached to, how would we take an attachment post from the root site, and attach it to a post in another site across tables? The post parent ID is a post ID, and doesn't contain a site or network ID.

So it is not possible. You could try and get some hacks to insert the URL, but it would be a great amount of effort and hassle.

You're also left with issues, for example, changes to the meta, titles, etc, dont carry across sites, if an image is deleted it wont update across sites, galleries won't work either, and you could mess up post <-> attachment relationships with post ID confusion. A lot of plugins and functionality won't work correctly. Featured images also saves the attachment ID, not the attachment URL, so that's going to be broken too

Why your old code no longer works

The old uploader was powered by thickbox and iframes, so your old code is hooking into the pages loaded for each tab. The new uploader however is not a thickbox and an iframe, it's powered by backbone.js and has been completely rewritten.

How I Would Do it Instead

I wouldn't access the main library. Instead I would sync the main library ( note the subtle distinction ). When you create an attachment post on the primary site, switch to each blog and duplicate it so it shows in those sites too. This allows all plugins and standard functionality to work as expected, and all the assumptions held bystandard operations hold still true, and you get what you wanted.

Following that, hook into post edit titles etc so that it updates the other sites on the network too.

Finally, activate the plugin on all sites, so that if an image is uploaded on a subsite, it's recreated on the primary site and the rest for all to see.

share|improve this answer
    
Thanks very much for your feedback. Unfortunately one of the main problems is that I am 'broadcasting' posts across a number of sites, which includes custom fields that reference an image via it's post_id. Should I literally sync them with the same ID, or add them so they get a new ID? And how could I manage conflicts? –  Ollie Dec 20 '12 at 9:48
    
You'd need a new ID to be safe, what kind of conflicts are you envisaging? –  Tom J Nowell Dec 20 '12 at 12:00
    
A conflict of IDs, that sounds fine though so I'll just have to keep an array stored as an option mapping old IDs to new IDs so I don't keep copying the image over each time. –  Ollie Dec 21 '12 at 10:32
    
Do it on the post save update delete hooks, and store the mapping as post meta, you shouldn't need to keep a massive array in an option. So do something like save the file path of the original file in a post meta, and do a WP_Query looking for it to test if it's already there, or save a post meta with the ID in the primary sites attachments post meta –  Tom J Nowell Dec 21 '12 at 10:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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