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.

  • I can’t get this to direct images to a folder…

    add_filter('acf/upload_prefilter/name=field_6140aa779827d', 'field_name_upload_prefilter');
    function field_nameupload_prefilter($errors) {
      echo '<pre>Hello world 1</pre>';
      // in this filter we add a WP filter that alters the upload path
      add_filter('upload_dir', 'field_name_upload_dir');
      echo '<pre>Hello world 2</pre>';
      return $errors;
    }
    
    // second filter
    function field_name_upload_dir($uploads) {
      // here is where we later the path
      $uploads['path'] = $uploads['basedir'].'/companies';
      $uploads['url'] = $uploads['baseurl'].'/companies';
      $uploads['subdir'] = 'logos';
      echo '<pre>Hello world 4</pre>';
      return $uploads;
    }

    Field type is Image, name “Company Logo” (“companylogo”, “field_6140aa779827d”).
    Aim is to upload a company logo but siphon it to a child of “uploads” – “uploads/companies/logos“.
    But it continues going to “uploads/2021/09“.

    None of my “Hello world” test lines are displayed, which may suggest the filter isn’t firing at all (?).

    Any thoughts?

  • Three more things:

    +1)
    I see from the doc, when a key is passed, the prefilter should take parameter “key=”, not “name=” – so I’ve changed this.

    +2)
    Even so, looking in debug.log, I see…
    [15-Sep-2021 12:11:20 UTC] PHP Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'field_name_upload_prefilter' not found or invalid function name in /Users/MyName/Sites/sitename/wp-includes/class-wp-hook.php on line 305

    +3)
    When I amend to correct the underscore typo (field_nameupload_prefilter to field_name_upload_prefilter, the warning is solved – the file successfully uploads… to /uploads/companies – but not /uploads/companies/logos. Ergo, I may be misunderstanding the role of subdir for wp_upload_dir.

    So, the following works nicely…

    add_filter('acf/upload_prefilter/key=field_6140aa779827d', 'field_name_upload_prefilter');
    function field_name_upload_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'].'/companies/logos';
      $uploads['url'] = $uploads['baseurl'].'/companies/logos';
      // $uploads['subdir'] = 'logos';
      return $uploads;
    }

    … I commented-out subdir and added the full path to basedir and baseurl. Is this acceptable?

  • From everything I see in the examples by others here https://developer.wordpress.org/reference/functions/wp_upload_dir/ they are doing what you are doing. $uploads[‘path’] contains the sub directory as well.

    I don’t know how it works, I think that subdir is only use if you have the media library set to group uploads by date.

Viewing 3 posts - 26 through 28 (of 28 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.