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?
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
❓Ever wondered when and why ACF uses JSON instead of the database? Check out our summary of the most recent session of ACF Chat Friday for the answer, and make sure to register for the next session.
— Advanced Custom Fields (@wp_acf) February 23, 2023
👉 https://t.co/3UtvQbDwNmhttps://t.co/wfFEVcXVKc
© 2023 Advanced Custom Fields.
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.