Support

Account

Home Forums General Issues Changing file upload path for acf field using upload_prefilter inside Multisite

Unread

Changing file upload path for acf field using upload_prefilter inside Multisite

  • Hi,

    I run a Multisite Network with 4 sites in a sub domain config. I created a custom post type for videos.

    I don’t want the video files to sit inside the wordpress folder structure, for various reasons, the main reasons being the size of my backups and the location being inside a custom CDN.

    Now, what I have done works perfectly for the main site, because it uses /content/uploads. So, when I upload a video into a video post, it saves that video file to /content/uploads/videos, /content/uploads/videos has been symlinked to my cdn – /mnt/cdn/videos (www-data has control). So the large video files goes into the cdn, away from the wordpress folder structure. All good.

    https://main.site/content/uploads/videos/911-trillions-follow-the-money-media.webm

    But the sub sites, by default uses /content/uploads/sites/x. If I use the same config for the sub sites, the video file is saved to the correct location, but wordpress then assigns an incorrect file url.

    https://sub4.site/content/uploads/sites/4//var/www/main/content/uploads/videos/czechoslovakian-wolf-dog-media.webm

    This is the function:

    
    /** Set Video Upload Path ----------------------------------------------- **/
    
    function video_upload_filter($errors, $file, $field) {
        
        add_filter('upload_dir', 'video_upload_dir');
        
    }
    
    add_filter('acf/upload_prefilter/key=field_6075238de0b09', 'video_upload_filter', 10, 3 );
    
    function video_upload_dir($param) {
        
        if ((get_field('sys_site_slug', 'option') == 'main')) {
            $param['path'] = '/var/www/main.site/content/uploads/videos';
            $param['url'] = 'https://main.site/content/uploads/videos';
        }
    
        if ((get_field('sys_site_slug', 'option') == 'sub1')) {
            $param['path'] = '/var/www/main.site/content/uploads/videos';
            $param['url'] = 'https://sub1/content/uploads/videos';
        }
    
        if ((get_field('sys_site_slug', 'option') == 'sub2')) {
            $param['path'] = '/var/www/main.site/content/uploads/videos';
            $param['url'] = 'https://sub2/content/uploads/videos';
        }
    
        if ((get_field('sys_site_slug', 'option') == 'sub3')) {
            $param['path'] = '/var/www/main.site/content/uploads/videos';
            $param['url'] = 'https://sub3/content/uploads/sites/4/videos';
        }
    
        return $param;
    }
    

    I have tried many configurations, without success.

    I am starting to think that the upload_prefilter is not doing it’s work when it comes to the sub sites like it does for the main site, when the upload_prefilter is called for the defined acf field.

    This all sounds complicated, but if we just look at how upload_prefilter deals with multisite sub sites, we will know.

    Help would be much appreciated.

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.