开发者

Wordpress 3 Multisite with same Medialibrary

i created a Worpress 3 Multisite with 5 Sub-Blogs. Is it possible to share the same Media-Library in this Blogs?

i changed upload_path in wp_1_options and wp_2_op开发者_开发问答tions for example and also in my backend in "Super Admins" Menu but it has no effect.

The files are uploaded to wp_contents/blogs.dir/1-2-3/files and the options have no effect.

any ideas? thanks!


One way around is to hook onto the load events of all media admin files, and switch to the main blog using switch_to_blog(1).

This would mean in any blog admin, the media library will always run as if it were on the main blog.

Note that a couple caveats include;

  • The media library for all blogs is stored in the main blog database table.
  • You may run into problems with inserting media into posts outside the main blog admin
  • You will run into problems with inserting galleries into posts outside the main blog admin
  • User permissions may be false positives or negatives

My best advice would be to use the code example below, and have a good play around with blog admins, logged in as different users, with different roles, and see what happens.

function use_main_blog_library()
{
    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');


In my searching on this topic, several posts lead back to this one, so I figured I'd share an idea that might help someone wanting to develop a suitable plugin to solve this...

  • Use get_site_option() and update_site_option() to store global plugin options.
  • Add an option via hooks to choose if a media upload should be shared network-wide and let the plugin track which media files and where they're located at.
  • Again, using hooks, have the shared items show up in each blog's media library and perhaps add an indicator showing the file is a network share.

I have spent several hours playing with the administrator hooks and filters and this could be accomplished through them, although I'm not savvy enough to know how to fully integrate it with all of the media library features.

The Shiba Media Library Plugin could serve as a valuable reference since they have utilized several custom features for the Media Library via hooks and filters.

I really wish I had the spare time to work on this right now because I would take my best shot at it. I hope this helps someone else.


I have found a possible solution, which works for me in WP3.7.1 (I haven't tested it in earlier versions)

Create a filter, which overrides the default upload directories:

add_filter('upload_dir', 'ms_global_upload_dir');

function ms_global_upload_dir($uploads)
{
    $ms_dir = '/sites/' . get_current_blog_id();

    $uploads['path']    = str_replace($ms_dir, "", $uploads['path']);
    $uploads['url']     = str_replace($ms_dir, "", $uploads['url']);
    $uploads['basedir'] = str_replace($ms_dir, "", $uploads['basedir']);
    $uploads['baseurl'] = str_replace($ms_dir, "", $uploads['baseurl']);

    return $uploads;
}

Important: the 'Upload Url Path' settings should be empty in Site Settings or if you need to customize it, check the results by dumping the $uploads array to view possible conflicts.

To check if your version of WP supports this method, locate function wp_upload_dir() in file wp-includes/functions.php and find function call: $uploads = apply_filters( 'upload_dir' ... If it presents, the solution above should work.

Hope, this helps ...

Additionally, I have spent almost two days to make a solution to replicate/delete the uploaded media in each of the blogs with action hooks 'add_attachment' and 'delete_attachment' by generating the necessary post and postmeta entries in the corresponding database tables. With this, you can add/delete media in any of the blogs, that will be visible in/removed from all blogs media library. If you are interested, I can share it...

Cheers

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜