Please see this comment for updated information about this filter.

If you have a project for a client or perhaps just for yourself, you may find that you need to upload a file in the WordPress back-end that is not supported by the core application.

Depending on the type of file that you want to introduce, you may need to add support for additional MIME types. Luckily, this is easy enough to do.

MIME Types in WordPress

No, not that type of mime.

But before looking at the code for how to do it, I think it’s important to understand exactly what we’re adding (otherwise, we run the risk of copying and pasting code and not knowing what it is or what it does other than it works).

MIME Types in WordPress

I could write a much lengthier article about what MIME types are, but that’s not this article. A general definition for this might be:

A way of identifying files on the Internet according to their data and their format.

Furthermore, here’s a quick primer:

  • MIME is an acronym (because, as programmers, we love acronyms) that represents “multipurpose internet mail extensions.”
  • The HTTP protocol which is used to transfer data across the Internet uses the content header of the MIME type it’s handling so that it knows how to handle it.

So, in our case, if we’re uploading a file to WordPress and it doesn’t support a specific MIME type, then that means the software doesn’t know what to do with it. For example, if you’re working with CAD files (specifically, those in the format of DXF, DWG, SKP, and so on).

For example, if you’re working with CAD files (specifically, those in the format of DXF, DWG, and so on). At this point, we’d need to introduce code that would allow WordPress to support the uploading and management of this type of file.

Given that WordPress already has a robust way of taking files and placing them in a proper directory (changing that is a post all its own), we just need to tell WordPress the additional MIME types to recognize.

Here’s how:

Given the code comments and what’s happening in the body of the function, it should be relatively clear as to what’s happening. Here is a clear walkthrough of the code:

  • We define a custom function that’s hooked into WordPress’ upload_mimes filter.
  • The function accepts an array that contains the supported MIME types,
  • We merge the existing array with a new array that includes a key for the file type and the MIME type for that kind of file,
  • The resulting array is returned.

As I said at the beginning, I don’t think it’s enough just to copy the code and use it. Understand what you’re working with and then go from there.

Ultimately, that’s all that’s required to update the supported MIME types in WordPress.

Category:
Articles
Tags:

Join the conversation! 3 Comments

  1. Hi Tom

    Even though this works, there’s actually a comment in core telling us not to use the upload_mimes filter to add to the list of allowed MIME types; instead, we should be using mime_types. According to the inline doc for the mime_types filter it’s mime_types to add to the list and upload_mimes to remove.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.