The question:
I currently use a WordPress network in order to run a number of sites, which all use the same template, plugins and images. As posts appear on a multiple sites, it makes sense to only upload images to one location (and have one copy of an image), rather than multiple ones.
Could I perhaps achieve this by creating a custom rewrite? Or is there a better way to achieve this?
Background information
I am using WPThree Broadcast to ‘broadcast’ an image over multiple sites, and therefore also copy the images into their respective image libraries. I don’t want the server to get cluttered with ten copies of the same image so instead am checking to see if the image is already in a site’s wp_posts
table, and then if not copying the post
and post_meta
data into that site’s tables.
However, I do not wish to copy the actual image into a new upload folder, as the default WP is configured to do (e.g. into /wp-content/blogs.dir/2/files/
), so instead I want all images to be uploaded to /wp-content/uploads/
, the default upload folder for a standard WP installation.
The Solutions:
Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.
Method 1
You can pretty simple add some actions to archive this.
function wpse_16722_main_uploads() {
switch_to_blog(1);
}
add_action('load-media-new.php', 'wpse_16722_main_uploads');
add_action('load-media-upload.php', 'wpse_16722_main_uploads');
add_action('load-media.php', 'wpse_16722_main_uploads');
add_action('load-upload.php', 'wpse_16722_main_uploads');
add_action('admin_init', 'wpse_16722_main_uploads');
This will change the current sub-blog to user your main-site with the help of switch_to_blog and of course you want the main side by the id 1.
You can also add a function to rewrite the upload folder to only /uploads without the subdirs:
function wpse_16722_upload_dir( $args ) {
$newdir = '/';
$args['path'] = str_replace( $args['subdir'], '', $args['path'] ); //remove default subdir
$args['url'] = str_replace( $args['subdir'], '', $args['url'] );
$args['subdir'] = $newdir;
$args['path'] .= $newdir;
$args['url'] .= $newdir;
return $args;
}
add_filter( 'upload_dir', 'wpse_16722_upload_dir' );
Method 2
In the post “How to Change the Default Media Upload Location in WordPress 3.5” by Editorial Staff, they show how to change the upload dir in wp-config.php
:
define( 'UPLOADS', 'wp-content/'.'files' );
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0