Support

Account

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

  • 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?