• Is there a way to change the default upload directory for specific file types? I would like to keep the image files in their current directory, but would like to have pdf files to be in a more generic directory. I wasn’t sure if its possible to modify the function code to move the default directory to only do it with certain file extensions/types.

Viewing 1 replies (of 1 total)
  • Hi @greencrest
    Yes, it is possible to change the default upload directory for specific file types in WordPress. You can use the wp_handle_upload_prefilter filter to modify the default upload directory based on the file type.

    Here is an example of how you can use this filter to change the default upload directory for PDF files:

    add_filter( 'wp_handle_upload_prefilter', 'custom_upload_directory' );
    
    function custom_upload_directory( $file ) {
        // Check if the file is a PDF
        if ( $file['type'] == 'application/pdf' ) {
            // Set the upload directory to a custom location
            $file['url'] = '/wp-content/uploads/pdfs/' . $file['name'];
            $file['error'] = false;
        }
     
        return $file;
    }

    This code will check if the uploaded file is a PDF. It will set the upload directory to a custom location for PDF files if it is. All other file types will continue to be uploaded to the default WordPress upload directory.

    You can modify this code to change the default upload directory for other file types or to specify multiple custom upload directories based on different file types. Give it a try, and let me know how that goes! 😄

Viewing 1 replies (of 1 total)

The topic ‘change default upload for certain file types’ is closed to new replies.