Home › Forums › Backend Issues (wp-admin) › 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.
I creating a form using various ACF fields.
For the “file” field, I want to customize the folder location for each of the file fields.
I have 3 scenarios:
Simple file upload field with name file1 – should be located to wp-content/uploads/simple-files
File field with the group field – should be located to wp-content/uploads/groups
Field Structure: Group 1 -> File 1 AND Field Path: uploads/groups/group1/file1
Field Structure: Group 1 -> File 2 AND Field Path: uploads/groups/group1/file2
File field with group field and the repeater field – should be located to wp-content/uploads/group-repeat
Field Structure: Group 1-> Repeater -> File 1 AND Field Path: uploads/groups/group-repeat1/repeater1/file1
Field Structure: Group 1-> Repeater -> File 2 AND Field Path: uploads/groups/group-repeat1/repeater1/file2
Please help he understand how I can achieve this using ACF hooks.
The topic ‘Change file upload path for one specific field.’ is closed to new replies.
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!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.