Changing the default upload location for multisite to customm map

Hi there,

If I use define(‘UPLOADS’, ‘wp-content/uploads/myimages’); in my theme's functions.php file, I get the new directory myimages into which my new uploads go. This is OK but for the rest of my subdirectory sites I want the structure to look like: uploads/sites/*site number*/myimages/ as opposed to what I expect would look like uploads/myimages/sites/*site number*/.

Could I use something like define(‘UPLOADS’, ‘../../myimages/’); to achieve the structure described above?

Thanks - Mark

  • Mark
    • The Incredible Code Injector

    Hi Jude,
    Thanks for the reply. I have had a look at the links you posted and they might help if...

    I want to change the existing structure from :
    /var/lib/wp_gooda/wp-content/uploads/sites/2/year/month
    to:
    /var/lib/wp_gooda/wp-content/uploads/sites/2/_mediavault/year/month

    Could you please take a look at the following and suggest the correct way to do..? I had thought of sticking this in my child theme's functions.php but that doesn't work. Also how would this then be applied to subsequent sites in the network who might use a different theme?

    add_filter('upload_dir', 'ml_media_upload_dir');
    
    /**
     * Changes the upload directory to what we would like, instead of what WordPress likes.
     *
     *
     */
    function ml_media_upload_dir($upload) {
    	global $user_ID;
            $blog_id = get_current_blog_id();
    	if ((int)$user_ID > 0) {
    		$upload['subdir'] = "/wp-content/uploads/sites/".$blog_id."/". get_the_time('Y'). "/" .get_the_time('m');
    		$upload['path'] .= $upload['subdir'];
    		$upload['url'] .= $upload['subdir'];
    	}
    
    	return $upload;

    Thanks for any suggestions - Mark

  • Mark
    • The Incredible Code Injector

    HI Jude,
    I have installed the code as a mu-plugin but I'm still having trouble with the directory structure.

    I need the directory /_mediavault/ to appear above /year/month. That is just after the site ID.

    eg wp-content/uploads/sites/2/_mediavault/year/month

    All I seem to be able to achieve is:
    wp-content/uploads/sites/2/year/month/_mediavault/file.xxx

    Any suggestions welcomed.
    Thanks again - Mark

  • Mark
    • The Incredible Code Injector

    HI again Jude,

    define( 'UPLOADS', '...'); allows me to insert a custom directory into the path prior to the sites/2/ part of the url but I need it after this part and before the year/month part.

    Thanks for your continued help,
    Mark

  • Michael Bissett
    • Recruit

    Hey @Mark, Michael here!

    After doing some additional poking at this, you should find this snippet to do the trick:

    <?php
    
    add_filter('upload_dir', 'ml_media_upload_dir');
    
    /**
     * Changes the upload directory to what we would like,
     * instead of what WordPress likes.
     */
    function ml_media_upload_dir($upload) {
            $upload['path'] = $upload['basedir'] . '/mediavault/';
            $upload['url'] = $upload['baseurl'] . '/mediavault/';
    
        return $upload;
    }

    Could you give that a try please, and let us know if that works?

    Kind Regards,
    Michael

  • Mark
    • The Incredible Code Injector

    Hi Michael,

    This is starting to work.

    The last piece of the puzzle is to add a year and month directory as this is removed by the code above. I'm not sure why this is not working. Do you know how to do this?

    $year = the_date('Y',false);
             $month = the_date('m',false);
             $upload['path'] = $upload['basedir'] . '/_mediavault/'.$year. '/' .$month);
             $upload['url'] = $upload['baseurl'] . '/_mediavault/'.$year. '/' .$month);

    Thanks for the ongoing assistance.
    Mark

  • Mark
    • The Incredible Code Injector

    Below works...

    add_filter('upload_dir', 'maxi_custom_upload_dir');
    
    /**
     * Changes the upload directory to what we would like,
     * instead of what WordPress likes.
     */
    function maxi_custom_upload_dir($upload) {
    
             $upload['path'] = $upload['basedir'] . '/_mediavault/'.current_time('Y'). '/' .current_time('m');
             $upload['url'] = $upload['baseurl'] . '/_mediavault/'.current_time('Y'). '/' .current_time('m');
    
        return $upload;
    }

    Thanks for the help :slight_smile:

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.