Adding MIME types to WordPress

Because of security reasons, WordPress has a whitelist of file types (so called MIME types) it allows you to upload in the media section in the backend.
Unfortunately, not all common files made it into this whitelist (yet). If you try to upload a SVG image for example or a v-card (.vcf), WordPress will present you with an error.
Just add the mime type you need to the $existing_mimes-array, using the correct key and value.


/*
* Add mime types to WordPress in order to be able to upload eg. vcards (.vcf), svg's etc.
* (Added line to htaccess as well in order to show a open/save dialogue when clicking the download link)
*/
add_filter('upload_mimes', 'custom_upload_mimes');
function custom_upload_mimes ( $existing_mimes=array() ) {
	// add extension to the array
	$existing_mimes['vcf'] = 'text/x-vcard';
	$existing_mimes['svg'] = 'image/svg+xml';
	$existing_mimes['webm'] = 'video/webm';
	return $existing_mimes; 
} 

Your web server also might need some help to give the right response for your more or less exotic mime type. Tell your server about your MIME types. For example, if you want to present vcards always as downloads to your visitors, add this to your .htaccess/nginx.conf file:

Apache .htaccess

# Addtype [mime type] [extension]
AddType application/octet-stream vcf

Nginx nginx.conf

include mime.types; # add your types after the include mime.tpyes line
types {
    application/octet-stream vcf  # [mime type] [extension]
}

Leave a Reply