Using same directory for storing all uploaded images on a WordPress network

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?

Read More

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.

Related posts

Leave a Reply

2 comments

  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' );