How to Change WordPress Upload Folder Programmatically?

I’ve got a custom theme that I’ve created an upload button where I call the WordPress Uploader when these buttons are pressed. Something like this:

jQuery('#ahng_blog_upload_button').click(function() { 
    formfield = jQuery('#ahng_blog_upload_image').attr('name');
    tb_show('Upload or Select Photo and Click on "Insert into Post"', 'media-upload.php?type=audio&TB_iframe=true');
    return false;
});

Right now, in my functions.php folder, I’ve set where I’d like the files to Upload (http://website.com/wp-content/uploads) but I want to change the Upload location depending on which button is pressed.

Read More

For example, when the ‘Upload Picture’ button is pressed, change the upload folder to images (http://website.com/wp-content/uploads/images), and when the ‘Upload MP3’ button is pressed, change the upload folder to audio (http://website.com/wp-content/uploads/audio).

I know I can upload everything to the default folder which I mentioned previously, but it’s easier to sort through later if I need to look for a specific file using ftp.

Is there a way to do that using jquery and ajax and calling a php file that doesn’t really return anything but makes changes to the Upload Dir in wordpress? Or any other way?

Related posts

Leave a Reply

1 comment

  1. The method I use is based in pure WordPress hooks, no jQuery or Ajax involved.

    And I really don’t know what buttons you are referring to… There’s been some time since the only button is “Upload Media”. Unless you are using customized ones.

    This WordPress StackExchange answer has other filtering possibilities. Bellow is that code adjusted to filter the uploads by media type.

    add_filter('wp_handle_upload_prefilter', 'wpse_25894_handle_upload_prefilter');
    add_filter('wp_handle_upload', 'wpse_25894_handle_upload');
    
    function wpse_25894_handle_upload_prefilter( $file )
    {
        add_filter('upload_dir', 'wpse_25894_custom_upload_dir');
        return $file;
    }
    
    function wpse_25894_handle_upload( $fileinfo )
    {
        remove_filter('upload_dir', 'wpse_25894_custom_upload_dir');
        return $fileinfo;
    }
    
    function wpse_25894_custom_upload_dir($path)
    {   
        // Determines if uploading from inside a post/page/cpt
        // If not, default Upload folder is used
        $use_default_dir = (
                isset($_REQUEST['post_id'] ) 
                && $_REQUEST['post_id'] == 0 
                ) 
                ? true : false; 
    
        if( !empty( $path['error'] ) || $use_default_dir )
            return $path; //error or uploading not from a post/page/cpt 
    
         // Save uploads in FILETYPE based folders. When using this method, 
         // you may want to change the check for $use_default_dir
         $extension = substr( strrchr( $_POST['name'], '.' ), 1 );
    
         switch( $extension )
         {
            case 'jpg':
            case 'png':
            case 'gif':
                $customdir = '/images';
                break;
    
            case 'mp4':
            case 'm4v':
                $customdir = '/videos';
                break;
    
            case 'txt':
            case 'doc':
            case 'pdf':
                $customdir = '/documents';
                break;
    
            default:
                $customdir = '/others';
                break;
         }
    
        //remove default subdir (year/month)
        $path['path']    = str_replace($path['subdir'], '', $path['path']); 
        $path['url']     = str_replace($path['subdir'], '', $path['url']);  
    
        $path['subdir']  = $customdir;
        $path['path']   .= $customdir; 
        $path['url']    .= $customdir;  
    
        return $path;
    }