15

I would like to change the upload directory for PDF files only, not for other files. For example, the PDF files would be uploaded to wp-content/uploads/pdf but other files would remain in wp-content/uploads/yyyy/mm.

I know it possible to filter upload_dir but I don't know how to do it with file types/mimes.

Thanks!

1

2 Answers 2

18

Following Justice Is Cheap lead, I ended adapting the functions from this plugin: http://wordpress.org/extend/plugins/custom-upload-dir/

<?php
/* 
 * Change upload directory for PDF files 
 * Only works in WordPress 3.3+
 */

add_filter('wp_handle_upload_prefilter', 'wpse47415_pre_upload');
add_filter('wp_handle_upload', 'wpse47415_post_upload');

function wpse47415_pre_upload($file){
    add_filter('upload_dir', 'wpse47415_custom_upload_dir');
    return $file;
}

function wpse47415_post_upload($fileinfo){
    remove_filter('upload_dir', 'wpse47415_custom_upload_dir');
    return $fileinfo;
}

function wpse47415_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 = '/pdf';
    $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;
}
1
  • Hi ... need to upload outside "/wp-content/uploads/" folder. Please help Commented Mar 30, 2016 at 10:23
1

you can change upload directory from wp-config.php file in wordpress.

define('UPLOADS', 'myimages'); Make sure you add this code before the line:

require_once(ABSPATH.’wp-settings.php’);

this is allows you to upload media in myimages folder instead of wp-content/uploads

Thanks

1
  • This doesn't answer the question. The initial problem was to change the upload folder for pdf files only.
    – Sinklar
    Commented May 11, 2017 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.