Home › Forums › Backend Issues (wp-admin) › Change file upload path for a specific field.
Hello, I found this post > https://support.advancedcustomfields.com/forums/topic/change-file-upload-path-for-one-specific-field/#post-35935
below is the code im currently using in my functions.php
add_filter('acf/upload_prefilter/key=field_627a6567e2078', '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'].'/protected';
$uploads['url'] = $uploads['baseurl'].'/protected';
$uploads['subdir'] = '';
return $uploads;
}
It still saves in the normal uploads folder rather than the /uploads/protected folder.
I have pro + This is not a repeater but a single field added to a specific page. I’m completely unsure what I’m doing wrong
I have also tried the following array solution but this didnt work either.
I had the same issue as @dtx211. I solved the problem by passing an array with the filters:
add_filter(‘acf/upload_prefilter/name=my_acf_upload_fieldname’, array( $this, ‘field_name_upload_prefilter’ ) );
AND
add_filter(‘upload_dir’, array( $this, ‘field_name_upload_dir’ ) );
Also ensure, that the upload directory has the right access rights and maybe already exists.
Another Update >
[10-May-2022 13:50:24 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 public_html/wp-includes/class-wp-hook.php on line 309
For anyone out there I found the solution. This no longer exists > 'field_name_upload_prefilter'
SO you need to change it to
'gist_acf_upload_dir_prefilter'
I found the solution here > https://gist.github.com/BODA82/a4b810b9c0f17fe4c52f7c7e96ade119
Here is my code Working as of today.
function gist_acf_upload_dir_prefilter($errors, $file, $field) {
// Only allow editors and admins, change capability as you see fit
if( !current_user_can('edit_pages') ) {
$errors[] = 'Only Editors and Administrators may upload attachments';
}
// This filter changes directory just for item being uploaded
add_filter('upload_dir', 'gist_acf_upload_dir');
}
// ACF hook, set to field key of your file upload field
add_filter('acf/upload_prefilter/key=field_627a6567e2078', 'gist_acf_upload_dir_prefilter', 10, 3 );
// Custom upload directory
function gist_acf_upload_dir($param) {
// Set to whatever directory you want the ACF file field to upload to
$custom_dir = '/uploads/protected';
$param['path'] = WP_CONTENT_DIR . $custom_dir;
$param['url'] = WP_CONTENT_URL . $custom_dir;
return $param;
}
oh good god i spent ages on this and ive just seen the error.
add_filter('acf/upload_prefilter/key=field_627a6567e2078', 'field_name_upload_prefilter');
function field_nameupload_prefilter($errors) {
one has a gap in the name …. the other doesnt
And of course it exists its a function name created. Full on developer brain meltdown today
You must be logged in to reply to this topic.
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.