Skip to content

Instantly share code, notes, and snippets.

@hmowais
Created December 18, 2019 11:25
Show Gist options
  • Save hmowais/a8b846780cec04b214ec1637a6479d4b to your computer and use it in GitHub Desktop.
Save hmowais/a8b846780cec04b214ec1637a6479d4b to your computer and use it in GitHub Desktop.
Upload All PDF Files into Separate Folder
<?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