Forked from BODA82/acf-change-file-upload-directory.php
Created
May 26, 2020 00:43
-
-
Save dgoze/67392cddedbea3d297196f2cbf4d7d88 to your computer and use it in GitHub Desktop.
Change the upload directory of Advanced Custom Fields file upload field.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment