Skip to main content

Allow custom file types upload in WordPress

WordPress doesn’t allow us to upload all kinds of files in media library, even for the zip files you get error: “Sorry, this file type is not permitted for security reasons.”
There is a quick way to solve this problem, add this snippet to your functions.php file:

add_filter('upload_mimes', 'add_custom_upload_mimes');

function add_custom_upload_mimes( $existing_mimes ){
  $existing_mimes['zip'] = 'application/zip';
  return $existing_mimes;
}

In this case zip is file type extension and application/zip is MIME Type.
If you want to allow other file types you just need to add them in $exisiting_mimes array.

Example:

add_filter('upload_mimes', 'add_custom_upload_mimes');

function add_custom_upload_mimes( $existing_mimes ){
  $existing_mimes['zip']  = 'application/zip';
  $existing_mimes['swf']  = 'application/x-shockwave-flash';
  $existing_mimes['rtf']  = 'text/richtext';
  $existing_mimes['tiff'] = 'image/tiff';
  return $existing_mimes;
}

Regarding MIME Types, you can find them by googling, good place to start can be sitepoint.com reference.

Further reading

Leave a Reply

Your email address will not be published.

Please don’t paste any HTML, Js or PHP code into comments box, use pastebin.com or similar service to share code examples.

Characters left: 1000