1. Home
  2. Knowledge Base
  3. WooCommerce Product Add-Ons Ultimate
  4. Filter file types
  1. Home
  2. Knowledge Base
  3. File Uploads
  4. Filter file types
  1. Home
  2. Knowledge Base
  3. Advanced
  4. Filter file types

Filter file types

If you’re using the file upload field type, you can change the permitted file types by using a filter. Please remember that allowing users to upload files to your server carries a security risk, so please use this feature with caution.

<?php
// Add PDFs to list of permitted mime types
function my_prefix_pewc_get_permitted_mimes( $permitted_mimes ) {
// Add PDF to the list of permitted mime types
$permitted_mimes['pdf'] = "application/pdf";
// Remove a mime type - uncomment the line below if you wish to prevent JPGs from being uploaded
// unset( $permitted_mimes['jpg|jpeg|jpe'] );
return $permitted_mimes;
}
add_filter( 'pewc_permitted_mimes', 'my_prefix_pewc_get_permitted_mimes' );
// Add PDF to the list of restricted filetypes
function my_prefix_pewc_protected_directory_allowed_filetypes( $restricted_filetypes ) {
$restricted_filetypes[] = 'pdf';
return $restricted_filetypes;
}
add_filter( 'pewc_protected_directory_allowed_filetypes', 'my_prefix_pewc_protected_directory_allowed_filetypes' );

Just add the above code and include the file types you wish to allow.

You can add this code snippet to your site by following these steps.

Other file types

Note that the code above works for file types that are natively supported by WordPress. If you want to upload files types that aren’t already supported by WordPress, you can filter upload_mimes:

function my_prefix_upload_mimes( $mimes ) {
// Add PhotoShop PSD files to list of permitted WordPress mime types
$mimes['psd'] = "application/x-photoshop";
return $mimes;
}
add_filter( 'upload_mimes', 'my_prefix_upload_mimes' );

Here’s a list of several file types, including PSD, AI, EPS, etc:

<?php
/**
*
*/
function new_prefix_pewc_get_permitted_mimes( $permitted_mimes ) {
$permitted_mimes['pdf'] = "application/pdf";
$permitted_mimes['svg'] = 'image/svg+xml'; //Adding svg extension
$permitted_mimes['eps'] = 'image/x-eps'; //Adding photoshop files
$permitted_mimes['ai'] = 'application/postscript'; //Adding photoshop files
$permitted_mimes['psd'] = 'image/vnd.adobe.photoshop'; //Adding photoshop files
$permitted_mimes['xd'] = 'application/zip'; //Adding photoshop files
$permitted_mimes['ps'] = 'application/postscript'; //Adding photoshop files
$permitted_mimes['xls'] = 'application/vnd.ms-excel'; // excel file
$permitted_mimes['xlsx'] = 'application/vnd.openxmlformats-officedocument.spreadsheetml'; // excel file
$permitted_mimes['csv'] = 'text/csv'; // csv file
return $permitted_mimes;
}
add_filter( 'pewc_permitted_mimes', 'new_prefix_pewc_get_permitted_mimes' );
function new_prefix_mime_types($mime_types){
$mime_types['svg'] = 'image/svg+xml'; //Adding svg extension
$mime_types['eps'] = 'image/x-eps'; //Adding photoshop files
$mime_types['ai'] = 'application/postscript'; //Adding photoshop files
$mime_types['psd'] = 'image/vnd.adobe.photoshop'; //Adding photoshop files
$mime_types['xd'] = 'application/zip'; //Adding photoshop files
$mime_types['ps'] = 'application/postscript'; //Adding photoshop files
$mime_types['xls'] = 'application/vnd.ms-excel'; // excel file
$mime_types['xlsx'] = 'application/vnd.openxmlformats-officedocument.spreadsheetml'; // excel file
$mime_types['csv'] = 'text/csv'; // csv file
return $mime_types;
}
add_filter('upload_mimes', 'new_prefix_mime_types', 1, 1 );
/**********************************************************
* Additionally, you can install the free plugin, WP Extra File Types:
* https://en-gb.wordpress.org/plugins/wp-extra-file-types/
* And enable various file types as required
**********************************************************/
view raw mime_types.php hosted with ❤ by GitHub

Was this article helpful?

Need More Support?

If you can't find the answer that you're looking for, then please use the link below to raise a support ticket.
Contact Support

Related Articles