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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 | |
**********************************************************/ | |