Categories
WordPress

Adding a Mime Type to WordPress

WordPress doesn’t have .xml as part of its default mime-types, so we need to add it ourselves. How can you do that?

Search for wp_get_mime_types() function in the WordPress core files. You will find it inside the wp-includes/function.php. In the function, you will find a filter called mime_types.

We have an appropriate filter now, and we just need to hook it up and add our new mime type to the existing mime type array. I’ve added .xml to this example, but if you need to add another mime type, just replace the values with your own.

Add the following to your functions.php:

function custom_upload_mimes( $existing_mimes ) {
	  // Add xml to the list of mime types.
	  $existing_mimes['xml'] = 'text/xml'; //You can add as per your requirement like : xml,svg,etc. 

	  // Return the array back to the function with our added mime type.
	  return $existing_mimes;
}
add_filter( 'mime_types', 'custom_upload_mimes' );

Please Note:

Do not replace the entire mime type array, you just have to add the above code to it.
Refresh your website and see the mime-type array (which is really long) with the new mime-type added.
And, to test that everything worked as expected, upload your XML file, which should now be uploaded perfectly.