Created
December 18, 2019 11:25
-
-
Save hmowais/a8b846780cec04b214ec1637a6479d4b to your computer and use it in GitHub Desktop.
Upload All PDF Files into Separate Folder
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 | |
| add_filter('wp_handle_upload_prefilter', 'pdf_pre_upload'); | |
| add_filter('wp_handle_upload', 'pdf_post_upload'); | |
| function pdf_pre_upload($file){ | |
| add_filter('upload_dir', 'pdf_custom_upload_dir'); | |
| return $file; | |
| } | |
| function pdf_post_upload($fileinfo){ | |
| remove_filter('upload_dir', 'pdf_custom_upload_dir'); | |
| return $fileinfo; | |
| } | |
| function pdf_custom_upload_dir($path){ | |
| $extension = substr(strrchr($_POST['name'],'.'),1); | |
| if(!empty($path['error']) || $extension != 'pdf') { return $path; } //error or other filetype; do nothing. | |
| $customdir = '/acf_attached_file'; | |
| $path['path'] = str_replace($path['subdir'], '', $path['path']); //remove default subdir (year/month) | |
| $path['url'] = str_replace($path['subdir'], '', $path['url']); | |
| $path['subdir'] = $customdir; | |
| $path['path'] .= $customdir; | |
| $path['url'] .= $customdir; | |
| return $path; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment