WordPress Multisite: How to define a common upload folder for all blogs or subsites

In WordPress multisite, media uploaded from the subsite users are stored to different subdirectories using blog id into uploads folder. But, if you need to save all media files in one folder rather than uploads folder with lots of child folders, then you are in right place.

Use the following code into your theme functions.php file if you think your theme won’t be changed. Otherwise, mu-plugins is the best option.

[php]
0) {
$upload[‘path’] = ABSPATH . ‘wp-content/images’;
switch_to_blog(1);
$upload[‘url’] = site_url() . ‘/’ . ‘wp-content/images’;
restore_current_blog();
$upload[‘subdir’] = “”;
$upload[‘basedir’] = $upload[‘path’];
$upload[‘baseurl’] = $upload[‘url’];
}
return $upload;
}

function make_filename_hash($filename) {
global $user_ID;
return $user_ID . time() . $filename; /*Using time() function for uniqueness*/
}
add_filter(‘sanitize_file_name’, ‘make_filename_hash’, 10);
[/php]

Enjoy!

Leave a Reply

Your email address will not be published. Required fields are marked *