In WordPress, you get the “Sorry, This File Type Is Not Permitted for Security Reasons” error when you try to upload certain files to the Media library. Similarly, in WooCommerce you may get the same error when you try to upload a downloadable product download files.
Why is that? Well, by default WordPress only allows certain file extensions to be uploaded to the site. For example PNG, JPG, PDF, PPT, DOC, MP3 and MP4 are within the allowed file types – the reason being they are “safe” and won’t contain malicious code that could create problems within a WordPress install.
The thing is – I’ve started selling my first downloadable product (a mini-plugin) here on Business Bloomer and I needed to upload a ZIP file (the mini-plugin), a JSON file (a Code Snippet export file for those who don’t like plugins) and a TXT file (the plugin’s raw code for those who like to play with PHP). All went smoothly for the ZIP upload, but as soon as I tried uploading the JSON and TXT files I got the “Sorry, This File Type Is Not Permitted for Security Reasons” error.
Panic? Not really. It’s only a matter of finding the right code to change this default WordPress behavior. So, say hello to the “upload_mimes” filter, which allows us to do just that. Enjoy!
PHP Snippet: Allow Any File Extension Upload @ WooCommerce Downloadable Product / WordPress Media Library
In the snippet below I’m allowing both JSON and TXT file uploads to WordPress. The first snippet tells WordPress to allow them, the second part is another requirement for certain file extensions as WordPress runs a validation upon Media upload and we need to make sure JSON and TXT are covered.
/** * @snippet Fix "Sorry File Type Not Permitted" @ WooCommerce Downloadable Product + WordPress Media Library * @how-to Get CustomizeWoo.com FREE * @author Rodolfo Melogli * @compatible WooCommerce 5 * @donate $9 https://businessbloomer.com/bloomer-armada/ */add_filter( 'upload_mimes', 'bbloomer_custom_mime_types' );function bbloomer_custom_mime_types( $mimes ) { if ( current_user_can( 'manage_woocommerce' ) ) { $mimes['txt'] = 'text/plain'; $mimes['json'] = 'text/plain'; } return $mimes;}add_filter( 'wp_check_filetype_and_ext', 'bbloomer_correct_filetypes', 10, 5 );function bbloomer_correct_filetypes( $data, $file, $filename, $mimes, $real_mime ) { if ( ! empty( $data['ext'] ) && ! empty( $data['type'] ) ) { return $data; } $wp_file_type = wp_check_filetype( $filename, $mimes ); if ( 'json' === $wp_file_type['ext'] ) { $data['ext'] = 'json'; $data['type'] = 'text/plain'; } elseif ( 'txt' === $wp_file_type['ext'] ) { $data['ext'] = 'txt'; $data['type'] = 'text/plain'; } return $data;}
WooCommerce: Fix “Sorry, This File Type Is Not Permitted for Security Reasons” For Downloadable Products .