Using one media library for a multisite network with WordPress 3.5 media library

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?

Related posts

Leave a Reply

1 comment

  1. 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.