Skip to content

Instantly share code, notes, and snippets.

Created October 24, 2016 23:42
  • Star 10 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save BODA82/a4b810b9c0f17fe4c52f7c7e96ade119 to your computer and use it in GitHub Desktop.
Change the upload directory of Advanced Custom Fields file upload field.
// ACF upload prefilter
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_580ac5acd5616', 'gist_acf_upload_dir_prefilter');
// 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/custom_dir';
$param['path'] = WP_CONTENT_DIR . $custom_dir;
$param['url'] = WP_CONTENT_URL . $custom_dir;
return $param;
Copy link

hmowais commented Dec 17, 2019

getting this error:
Unexpected response from the server. The file may have been uploaded successfully. Check in the Media Library or reload the page.

Copy link

vls-dev commented Feb 24, 2020

need to change ln 19 to...
add_filter('acf/upload_prefilter/key=field_580ac5acd5616', 'gist_acf_upload_dir_prefilter', 10, 3 );

Copy link

Thanks for the gist, it's really helped me from banging my head against the wall. Is it possible to also further sort uploads by user id in addition to the custom directory?

Copy link

flowbru commented May 24, 2022

is deleting the files working for you?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment