I’ve just started using HTML5 video on my website but when I tried to upload a .webm file to WordPress’ Media Manager, it presented me with this error:

webm-file-upload-error

It turns out that WordPress doesn’t have .webm as part of it’s allowed mime types by default, so we need to add it ourselves.

A quick search on the WordPress core files leads us to the wp_get_mime_types() function in wp-includes/functions.php. Inside this function, on line 1887, WordPress has provided us with a filter called mime_types, perfect.

wp-get-mime-types

Now that we have a suitable filter, we just need to hook onto it and add our new mime type to the existing mime type array. I’ve added webm in this example, but if you needed to add another mime type, you would simply replace the values with your own. Add the following to your functions.php:

Note: Many tutorials on this subject suggest using the upload_mimes filter, but if you’re not careful, you’ll actually replace the entire mime type array, rather than adding to it. This means you won’t be able to upload any file type, but the one you added! not exactly what we want.

If you want to double-check everything went ok, simply dump the contents of the wp_get_mime_types() function directly to your page using a WordPress action hook like template_redirect:

function my_theme_output_upload_mimes() {
    var_dump( wp_get_mime_types() );
}
add_action( 'template_redirect', 'my_theme_output_upload_mimes' );

Refresh your website and you’ll see the mime type array (which is really long) with the new mime type added.

The new mime type array
Our new .webm mime type, added to the existing mime type array

And of course to really test that everything worked as expected, upload your file to the media manager which should now upload perfectly.

7 comments

  1. Never thought of doing something like this before. This would be useful in future, good to know there’s a filter to add a new mime type.

Comments are closed.