WordPress handles media files through it’s Media Library and in it’s default installation only certain file types, or better said: MIME-types, are allowed to be uploaded.
The current selection of allowed file types (ie.the file types that are not banned) can be rather limiting specially when you’re using bbPress as a forum wiht your WordPress setup. I’m not sure about the need for additional formats for all bbPress forums, but in the Tweaking4All forum I most certainly would like to be able to upload RAR, 7z, GZIP, XML and CSV files (amongst others).
Unfortunately WordPress does not offer a straight forward interface in the Admin Pages to modify the allowed Files Types.
In this article a short piece of code to allow you to add other file types or so called mime-types.
Standard Support File Types in WordPress
WordPress supports a standard set of file or MIME types (a.k.a. file types or Internet Media types) – I’m sure this list changes over time, so consider looking at the source of this information when looking for what files to add or not in the WordPress Accepted FileTypes page or the WordPress Codex on Uploading Files.
Currently, at the time that I’m writing this article, these formats are allowed:
Images: .gif, .jpg, .jpeg, .png
Documents: .pdf, .doc, .docx, .ppt, .pptx, .pps, .ppsx, .odt, .xls, .xlsx, .zip
Audio: .mp3, .m4a, .ogg, .wav
Video: .mp4, .m4v, .mov, .wmv, .avi, .mpg, .ogv, .3gp, .3g2
Please keep in mind that files might be rejected based on other criteria, for example lack of disk space, because the file is larger than the maximum allowed file size or because your web-host does not allow particular files.
Note : If you’d like to allow larger files, you’ll need to edit the PHP configuration file (php.ini) and add or set the following lines in php.ini (the example allows files up to 32 Mb):
upload_max_filesize = 32M
post_max_size = 32M
Add Allowed File Types to WordPress
The approach in this article is straight forward: We will ADD file/MIME types to the existing list of allowed file types.
PAY ATTENTION TO SECURITY RISKS
Before adding random file/MIME types: please think about possible security issues.
Only add file types that you REALLY need and that you are comfortable with.
A full list, maintained by IANA, of MIME types can be found in the Media Types Overview.
The following code must be added to the
functions.php file of your theme.
It will add the following file types:
Data files: CSV, XML,
Compressed files: 7z, RAR, TAR, TGZ, ZIP, GZ, GZIP
Application packages: APK, DEB, RPM
Disk images: IMG, ISO
Fonts: TTF, WOFF
return array_merge($existing_mimes, array(
'csv' => 'application/octet-stream',
'xml' => 'application/atom+xml',
'7z' => 'application/x-7z-compressed',
'rar' => 'package/rar',
'tar' => 'package/x-tar',
'tgz' => 'application/x-tar-gz',
'apk' => 'application/vnd.android.package-archive',
'zip' => 'package/zip',
'img|iso' => 'package/img',
'gz|gzip' => 'package/x-gzip',
'deb|rpm' => 'package/x-app',
'ttf|woff' => 'application/x-font') );
In essence it takes the existing array of allowed file types ($existing_mimes) and we simply add the ones we want additionally. The “new” array will then be returned to WordPress.
Don’t forget that this is only EXAMPLE code – you’ll have to modify it to match your needs.
And that’s all folks …