Support

Account

Home Forums Feature Requests Uploaded Files Location Reply To: Uploaded Files Location

  • This is how i was able to change the uploads directory for a specific ACF field. I have left some extra code for debugging.

    1 . acf/upload_prefilter/name=my_acf_upload_fieldname’ change my_acf_upload_fieldname to the name of your ACF field.
    2. $mydir = ‘/newdirectory’; // this currently writes to /uploads/newdirectory change newdirectory to your directory name.
    3. my_acf_upload_prefilter is called before the uploading of a file (whatever you name your ACF file field). Then it calls add_filter(‘upload_dir‘, ‘my_upload_directory’); which changes the directory for the upload of that file. but otherwise files upload to whatever is your default wp directory.

    function my_acf_upload_prefilter( $errors, $file, $field ) {
        
        // only allow admin
        if( !current_user_can('manage_options') ) {
            
            // this returns value to the wp uploader UI
            // if you remove the ! you can see the returned values
            $errors[] = 'test prefilter';
            $errors[] = print_r($_FILES,true);
            $errors[] = $_FILES['async-upload']['name'] ;
            
        }
        //this filter changes directory just for item being uploaded
        add_filter('upload_dir', 'my_upload_directory');
        
        // return
        return $errors;
        
    }
    add_filter('acf/upload_prefilter/name=my_acf_upload_fieldname', 'my_acf_upload_prefilter');
    
    function my_upload_directory( $param ){
        $mydir = '/newdirectory';
    
        $param['path'] = $param['basedir'] . $mydir;
        $param['url'] = $param['baseurl'] . $mydir;
    
    	// if you need a different location you can try one of these values
    /*	
        error_log("path={$param['path']}");  
        error_log("url={$param['url']}");
        error_log("subdir={$param['subdir']}");
        error_log("basedir={$param['basedir']}");
        error_log("baseurl={$param['baseurl']}");
        error_log("error={$param['error']}"); 
    */
    
        return $param;
    }