Modifying Allowed Upload Types in WordPress

Several times recently in #wordpress, I’ve seen people asking how they could modify the list of allowed file types used in the file uploader on the Write Post page.

Since this information isn’t readily available (apparently) and to a lesser degree because I’m tired of not being able to find my previous code (causing me to re-type it all each time), I thought I’d throw together another quick WordPress hack guide.

Introduction

When you attempt to upload a file in WordPress (2.0+ I believe) that is not in the default list of acceptable file types1, you will receive the following error:

WordPress File Upload Error

As of WordPress 2.2, there are 35 allowed file types configured in the default install. While there’s no admin-based tool for editing this list (nor any plugins that I’m aware of), it’s not at all difficult to add your own…

The Code

Upload filetypes are checked by the function wp_check_filetype in wp-includes/functions.php (around line 1,000 in my current copy of trunk). Looking at the code, we see that the default array is passed into the upload_mimes filter, allowing you to easily add and remove types at will using a quick plugin hook.

So how do we do it? Well, first you need to add a new plugin hook. In your theme’s functions.php file, add this line:

add_filter('upload_mimes', 'custom_upload_mimes');

You can, of course, replace custom_upload_mimes with your own preferred function name. Just make sure it’s something unique that ideally won’t cause any naming conflicts later on2.

Now we’ve got a hook that tells WordPress to take the array of file types passed into the upload_mimes hook and hand it to the function custom_upload_mimes. Great, but where’s our function?

No problem, I’ve got it all ready for you. Open back up your theme’s functions.php file and toss in this code:

function custom_upload_mimes ( $existing_mimes=array() ) {
 
// add your ext => mime to the array
$existing_mimes['extension'] = 'mime/type';
 
// add as many as you like
 
// and return the new full result
return $existing_mimes;
 
}

Note that the function accepts the $existing_mimes array, adds a new file type (with the extension “extension” and of the mime type “mime/type”), and then returns the whole array.

Replace extension with your extension (no period before it, just the textual extension) and then Google to find out its mime type3.

Add as many new types as you like, simply by copying the example line and filling in your values. Also, make sure you name the function the same thing you used in the hook, assuming you don’t like my convention. Save your new functions.php file and you’re good to go!

Removing Existing Types

What if you want to remove an existing allowed type, instead of adding your own new type? Well, that’s even easier!

Replace the line $existing_mimes['extension'] = 'mime/type'; with unset( $existing_mimes['extension'] ) and you’re done. For example, to prevent users from uploading .exe files, you would use:

unset( $existing_mimes['exe'] );

Good luck, hope this helps!

  1. For example, Microsoft Installer files are not allowed by default, so track down an .msi file and try to upload it for a quick test. []
  2. I like to use the prefix ‘custom_’ simply because it’s easy to tell later on that this is a custom modification. []
  3. Googling for “.zip mime type”, for example, should give you any number of sites listing a whole slew of mime types. Yours should be in the same <category>/<type> format as the others. Zip is “application/zip”. []
Posted in Uncategorized | Tagged , , , , , , , | 19 Comments

19 Responses to Modifying Allowed Upload Types in WordPress

  1. Anonymous says:

    I realize this was posted about two years ago, but it doesn’t work with WP 2.7.1, any ideas on how to make it work?

    Thanks

  2. Pashmina says:

    me too… I’d like to know how to add custom mime types.

  3. Chris Meller says:

    Looking at the latest trunk code of WP 2.9-pre, it looks like the same code should still work.

  4. Pingback: Question about Ebooks - WordPress Tavern Forum

  5. Thanks; this would be much improved if you could at least give a single example of one file type, like .xml, which I’m trying to add now.

  6. Pingback: How to get around WordPress’ limitation on uploading certain file types | WP Garage

  7. Pingback: Cambiar tipos de archivo que se pueden subir | Ayuda WordPress

  8. enjoy says:

    It works on WP 2.9.x like a charm :) Thank You!

    • Raz says:

      enjoy, I’d wanted 2 upload a .WMF file. I made a one line addition to functions.php (WP 2.9.2) –
      if ( !$mimes ) {
      // Accepted MIME types are set here as PCRE unless provided.
      $mimes = apply_filters( ‘upload_mimes’, array(
      ‘wmf’ => ‘application/x-msmetafile’,
      ‘jpg|jpeg|jpe’ => ‘image/jpeg’,
      ‘gif’ => ‘image/gif’,

      I made no other changes.

      But the problem is that after uploading(without that error), from Upload New Media, the image is not being displayed at all. In the editor only the filename is being displayed.

      What to do?

      • touchwood says:

        That’s because WMF is not a supported image format.. Therefore this hack lets you upload it as an attachment to be downloaded, but does not handle it as an image with thumbnail creation etc.

  9. Pingback: Alterar os tipos de arquivo podem ser inseridos | Ajuda Wordpress em Português

  10. Pingback: Adding RAR to allowed upload types of wordpress | ~ overflow ~

  11. Pingback: Shifting Daniel Jaber into WordPress | supernaut

  12. Pingback: things with bits » Shifting Daniel Jaber into WordPress

  13. Damn, that sound’s so easy if you think about it.

  14. Pingback: How to get around WordPress’ limitation on uploading certain file types « If you want to get along, Go along.

  15. prime says:

    I got the following message:
    Fatal error: Call to undefined function add_filter() in C:xampphtdocsw-pwp-includesfunctions.php on line 25

  16. Thanks! sooo easy to do too. Worked right away.

  17. Pingback: » Modify Wordpress to Accept New Filetypes as Uploads

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">