What to do when Wordpress says this file type is not permitted for security reasons


wordpress-logo-stacked-rgb.pngIn Wordpress, have you tried uploading a file to a post only to be told "Sorry, this file type is not permitted for security reasons". Or maybe the file upload silently fails with no visible message, and nothing attached to your post? In my case I'd defined a Book post type (using the PODS framework) and added a bunch of file upload fields to support different file extensions. Uploading a PDF file worked fine, while uploading an EPUB file either silently failed or gave me this "Sorry, this file type is not permitted for security reasons" message.

The issue turns out to be easy to solve, and the recommended solution gives you flexible support for many file types not supported by Wordpress out of the box.

Turns out that Wordpress has a list of allowed file types. The error message comes from the '_wp_handle_upload' function in wp-admin/includes/file.php, and just before the error message are many checks against the file MIME type.

Reminder:- the MIME type concept was invented in the early 90's to enable multi-media multi-part rich media e-mails. Before the MIME addition to Internet e-mail, email was pretty darn plain. The people developing HTTP borrowed MIME types in developing web protocols which is why we use MIME types all through web development work -- like, CSS files are identified as text/css, and JPEG's are identified as image/jpeg.

The MIME type for EPUB files is application/epub+zip, by the way.

There is a filter function, upload_mimes, that can extend the list of MIME types recognized by Wordpress - see: http://codex.wordpress.org/Plugin_API/Filter_Reference/upload_mimes

I tried implementing this function in my functions.php but that didn't work out.

Searching in the Wordpress plugins I found several which promised to extend the list of MIME types. I choose the "WP Extra File Types" plugin.

It's settings page is simple - it lists off a bunch of file types, along with the extension, and a checkbox for each. Simply check the ones you want, click the SUBMIT button at the bottom of the page, and voila you can now upload files of that type.