Support

Account

Home Forums Feature Requests Uploaded Files Location

Solving

Uploaded Files Location

  • I would like to upload file attachments to my custom-post type, but NOT store them in the WordPress gallery.

    I’d like to specify a location to put them within the field settings.

    For example, if I specify a folder called “artwork”, then the files would be uploaded to “uploads/acf/artwork”. This would also NOT index it into the Gallery.

    The gallery is way too cluttered as it is.

  • I’m sure this would be useful but I think it’s going outside the scope of what ACF is intended for. I will flag this topic for the developer’s attention.

    For changing the upload path see this documentation. https://codex.wordpress.org/Plugin_API/Filter_Reference/upload_dir

  • One way might be using filters. Perhaps he can create hooks that I can use to set where the file goes and if it gets added to the gallery.

  • Hi @dnavarrojr

    Thanks for the thread.

    I wonder if it is possible to use the filet which @hube2 has linked to, and within the filter, look at the $_FILES and $_POST data to determine if the file was uploaded via ACF.

    This way you can conditionally change the upload dir.

    Let me know if you are able to play around with this, and what you find.

  • 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;
    }
  • That works. However, here’s my biggest issue…

    I am using ACF like crazy to create tons of extensions to our web site. And having the code separate from the “form” makes it difficult to maintain.

    The ability to attach the code for a given field to that field in the ACF builder would now only allow me to more easily maintain the code whenever I make changes, but it would allow me to export/import the associated code with each form.

    Maybe this is better off as an addon, but I would really love to see the ability to add JS and PHP code to each field in the ACF form builder.

  • Perfect, I’ve spent hours looking into download managers etc.. I’ve used this little script with a .htaccess and download file modification to protect only my ACF uploads for logged in users only.
    PM if anyone interested..

    .htaccess control, and download file.

  • This reply has been marked as private.
  • Is this method still working in the latest ACF version?

    I was working on a site locally until my database crashed last week. I used a customised version of your code for the last few weeks, but since I restored the site from a backup a few days ago I keep getting a 500 error when this function runs.

    Here’s my code:

    function my_upload_directory( $param ) {
      $mydir          = '/music';
      $param['path']  = $param['basedir'] . $mydir;
      $param['url']   = $param['baseurl'] . $mydir;
      return $param;
    }
    
    function my_acf_upload_prefilter( $errors, $file, $field ) {
      // change the upload directory
      add_filter('upload_dir', 'my_upload_directory');
      return $errors;
    }
    add_filter('acf/upload_prefilter/name=music-cover-art', 'my_acf_upload_prefilter');
    

    Any idea where the problem might lie? I’m using the exact same code that worked before, the only thing that has changed seems to be the ACF version (5.5.3 to 5.5.5)?

  • Just wanna add that I had trouble getting this to work aswell. But I’m using Sage theme from Roots, and you need to hook into the filters like this:

    add_filter('upload_dir', __NAMESPACE__ . '\\my_upload_directory');
    add_filter('acf/upload_prefilter/name=preview_video', __NAMESPACE__ . '\\my_acf_upload_prefilter');

    Otherwise it won’t be able to locate the filters.

  • I had the same issue as @dtx211. I solved the problem by passing an array with the filters:

    add_filter('acf/upload_prefilter/name=my_acf_upload_fieldname', array( $this, 'field_name_upload_prefilter' ) );

    AND

    add_filter('upload_dir', array( $this, 'field_name_upload_dir' ) );

    Also ensure, that the upload directory has the right access rights and maybe already exists.

  • Sorry to dig up an old post, but I can’t seem to get this to work with sub fields (through a repeater).

    I know John suggested the field key on another thread but I’ve had no luck.

    Thanks

  • You can help me with your script in latest WordPress version?

    Regards

  • Sorry for digging old post but i’m facing same issue. I keep getting a 500 error on “async.php” when this function runs. Without this filter i was able to upload files.

    I tried all possible solutions including
    – Memory increasing in .htaccess, php etc
    – passing array as parameter as suggested above
    – folder permission changed

    Is there anyone can help me on this ?

  • As it still is a top result in Google:

    The original code still works for the most part. What throws an error is this line:
    function my_acf_upload_prefilter( $errors, $file, $field ) {

    WordPress expects one parameter only here, so do this instead:
    function my_acf_upload_prefilter( $file ) {

  • This reply has been marked as private.
  • @blackfox123 Hey Dude I really need your help

  • Sorry to dig up an old post, but I can’t seem to get this to work with sub fields (through a repeater).

    I know John suggested the field key on another thread but I’ve had no luck.

    Just make sure that my_acf_upload_fieldname is your sub_field name (this should be a File field), not the repeater field name.

  • I was getting 500 errors:
    Uncaught ArgumentCountError: Too few arguments to function my_acf_upload_prefilter(), 1 passed in xxx/wp-includes/class-wp-hook.php on line xxx and exactly 3 expected

    because this line:

    add_filter('acf/upload_prefilter/name=my_acf_upload_fieldname', 'my_acf_upload_prefilter');

    needs the number of parameters:

    add_filter('acf/upload_prefilter/name=my_acf_upload_fieldname', 'my_acf_upload_prefilter', 10, 3);

  • acf/upload_prefilter/name=my_acf_upload_fieldname’ change my_acf_upload_fieldname to the name of your ACF field.

    For my_acf_upload_fieldname, how would you reference an Image field that is inside a Group? Does it matter?

  • I would use the field key variant, not the field name. When ACF runs hooks for sub fields the field name for the filter is generally the concatenated version of the field name "{$parent}_{$child}" but not always.

    In some cases even using the field key variant is not sufficient. In some cases I have had to use either to field type variant or even the all fields variant acf/upload_prefilter and then look at the values in the $field array and figure out a way to match to the field I want to filter.

    Anyway, I usually try the field key variant first and if that does not seem to work then I use one of the others and start trying to dissect what is being passed in $field in order to make it work.

Viewing 23 posts - 1 through 23 (of 23 total)

The topic ‘Uploaded Files Location’ is closed to new replies.