Sign up ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Is there a way I can set a custom upload folder for each media type? for example: .PNGs should be saved in uploads/png, .GIFs in upload/gifs

share|improve this question

1 Answer 1

[More info added]. See below the code.

//~ change upload dir
add_filter('upload_dir', 'choose_upload_dir');

//~ upload image
$upload = wp_handle_upload( $_FILES['your_form_input_name'], array('test_form' => false) );

//~ change upload dir back
remove_filter('upload_dir', 'choose_upload_dir');

function choose_upload_dir($upload) {
    //~ your file to upload
    $file_type = $_FILES['your_form_input_name']['type'];

    //~ switch between file types
    switch($file_type) {
        //~ if type is 'png' then upload subdir is '/png' and so on
        case 'image/png':
            $type_dir = '/png';

        case 'image/jpeg':
            $type_dir = '/jpg';

            $type_dir = '/others';

    $upload['subdir'] = $upload['subdir'].$type_dir;
    $upload['path'] = $upload['basedir'].$upload['subdir'];
    $upload['url'] = $upload['baseurl'].$upload['subdir'];

    return $upload;

More info: Casually I've stumbled upon useful plugin which allows you to setup different directories for your uploads: Relocate Upload

share|improve this answer
Any chance you're still out there listening to these comments, Max Yudin? I would love to get this code to work, but I get an error of "Fatal error: Call to undefined function wp_handle_upload()". After a bit of research on the wp_handle_upload function I found some sample code on the codex page and added it to the start of your code: if ( ! function_exists( 'wp_handle_upload' ) ) { require_once( ABSPATH . 'wp-admin/includes/file.php' ); }. The error goes away but still no luck w/ uploads being created in their own folder – codeview May 17 at 20:29
Found an answer in a related question:… – codeview May 17 at 21:43

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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