Support

Account

Home Forums Backend Issues (wp-admin) Change file upload path for one specific field.

Solved

Change file upload path for one specific field.

    • antanova

    • September 10, 2015 at 3:39 am

    I’m trying to create a site that has downloadable files – but access to the files will be restricted. So I want the files uploaded from one specific field to be uploaded to a folder of their own, without changing the upload paths for any other uploaded file.

    Is there any documentation about this anywhere that I should look at? Google brought up something similar, but without a way to identify which field a file is being uploaded from (and so then change the upload path).

    Thanks in advance.

  • I know this is a very old topic and you have either figured this our or moved on by now. This is for others that might be trying to do the same thing since I have recently done something similar.

    You need to add a couple of filters. The first is for the upload prefilter

    
    add_filter('acf/upload_prefilter/name=field_name', 'field_name_upload_prefilter');
    function field_nameupload_prefilter($errors) {
      // in this filter we add a WP filter that alters the upload path
      add_filter('upload_dir', 'field_name_upload_dir');
      return $errors;
    }
    // second filter
    function field_name_upload_dir($uploads) {
      // here is where we later the path
      $uploads['path'] = $uploads['basedir'].'/your_path_dir';
      $uploads['url'] = $uploads['baseurl'].'/your_path_dir';
      $uploads['subdir'] = '';
      return $uploads;
    }
    
  • That’s fantastic, thanks!

    I didn’t solve the problem myself, but instead worked around it a different way, but this will be useful in future, I’m sure.

    • CC

    • June 30, 2016 at 11:07 pm

    @hube2 – your function is missing an underscore in order to work correctly. field_nameupload_prefilter should be field_name_upload_prefilter.

    CC.

  • sorry, typo on my part, I copied that from a php class in one of my plugins and renamed the functions to post here.

    • AJD

    • July 5, 2016 at 3:43 am

    Thanks! This snippet to change the upload directory for an ACF field works like a charm!

  • @john Huebner

    Ok so I tried your script and it is not loading for me. Everything I upload is still going to default and when going to select file still pulling default and not teh custom field.

    My custom folder is /profiles/
    My ACF Field is dn_cv_link

    The function code is below.

    add_filter(‘acf/upload_prefilter/name=dn_cv_link’, ‘field_name_upload_prefilter’);
    function field_nameupload_prefilter($errors) {
    // in this filter we add a WP filter that alters the upload path
    add_filter(‘upload_dir’, ‘field_name_upload_dir’);
    return $errors;
    }
    // second filter
    function field_name_upload_dir($uploads) {
    // here is where we later the path
    $uploads[‘path’] = $uploads[‘basedir’].’/profiles/’;
    $uploads[‘url’] = $uploads[‘baseurl’].’/profiles/’;
    $uploads[‘subdir’] = ”;
    return $uploads;
    }

  • Is your path '/wp-content/uploads/profiles/' This is where files should be uploaded to. These filters will not stop the field from showing all of the files in the media library when selecting an image, it just alters where the images are uploaded to.

  • no I want them to be in website.com/profiles/

    so the profiles will be on root site

  • Even still it is uploading to /wp-content/uploads/2016/08/ not a sub folder

  • I can’t say why it’s not changing the upload path. What type of field is it? Is it a sub field of a repeater or flex field?

    If you want them in the root of the site then you’ll need to mess with the lines that set the path, to be honest I don’t remember what’s in the $uploads variable or what you need to change.

    
    $uploads[‘path’] = $uploads[‘basedir’].’/profiles/’;
    $uploads[‘url’] = $uploads[‘baseurl’].’/profiles/’;
    
  • field type is “File” file object library is set to all

  • If your file field is a subfield of a repeater or flex field, try using the field key instead of the field name in your hook.

    
    add_filter('acf/upload_prefilter/key=FIELD_KEY_HERE', 'field_name_upload_prefilter');
    
    • cjcort

    • August 11, 2016 at 11:22 pm

    Im also having some issues changing the base directory for a custom field to something other then the /wp-content/uploads/ folder path. Is there a solution to this??

  • I can’t help you with that, all I have to go on here is the WP documentations, which is limited https://codex.wordpress.org/Plugin_API/Filter_Reference/upload_dir

  • John,

    It is still picking up that field. IF you could help me we could do like a teamviewer or skype. My skype is dmyers7980. I have been trying for a week.

    • cjcort

    • August 12, 2016 at 12:16 am

    Worth a shot.. thanks for the quick reply!

  • Sorry, just saw this, I don’t have Skype and it wouldn’t help me if I did. I’m located way out in the boonies and I’m am limited to DSL. Yeah, it sucks, I’ll get high speed internet here about the same time that cows start watching TV.

    You might find this interesting, It’s very hard to figure out what’s going on during an AJAX call, or even if your function is being called, or if it is why it’s not working. I outlined the method I use for getting feedback during AJAX calls in this topic. If you get the upload working the rest of the conversation will help you figure out how to limit the query that shows the images that can be selected. https://support.advancedcustomfields.com/forums/topic/filter-gallery-items/

  • I’m stuck, I’m currently trying to modify the path to place uploads in the root directory so that downloads can be accessed by example.com/document.pdf

    Does anyone know how to modify this to achieve the results? I’ve played around with the path settings and base URL and can’t get it to work.

  • You may want to start a new topic for your question. Old topics don’t get much attention accept for those that have posted in them. You might get some fresh eyes on this with a new thread.

  • Thanks John!

  • It’s just that I don’t have any experience with trying to upload files into the root of the site. I’m not sure if it’s even possible to be honest, it could be that this is being prevented by WP somehow. You might also want to look on other forums for a solutions because this is not really specific to ACF. The only part of this that’s specific to ACF is adding the upload_dir filter on a specific field.

  • Hi! I was able to change the file upload path for my field like it’s explained here but now I wanted to filter the media library to show only the images uploaded to that folder when using the field. Is this possible?

  • Is it possible? Honestly, I don’t know, but think it is. I can’t give you specifics on doing it but I can try to point you in the right direction.

    When you click on the field to add an image ACF does an AJAX request that causes the media library modal to open.

    To filter the files shown you need to create a pre_get_posts filter https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts.

    In this filter you need to see if it is an AJAX request and you need to look at the $_POST values submitted. $_POST will contain the field key of the field that the request is being done for.

    Sorry I can’t give you any more details than this, it has been a very long time since I’ve looked at doing something like this. I do know that there is a forum topic that has more specific information…….

    Did another look, and found it https://support.advancedcustomfields.com/forums/topic/filter-gallery-items/

    • JMay

    • October 6, 2019 at 8:48 am

    What a time-saver! Thanks, John!

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.