There are various file types that are allowed to upload in WordPress. People often use image uploader and forget that not only images, but other file types are allowed to upload such as PDF, MS Office, OpenOffice, GZ, ZIP, etc. Sadly there's no official document that give a detailed description about this at the moment. I was trying to find any articles in the Internet for help, but I didn't succeed. So, I had to dig into the source code of WordPress, and found a solution. I'm writing it here because I think it might help someone like me.
The code I found is located in get_allowed_mime_types()
in /wp-includes/functions.php
file. Here's the code of this function, all allowed file types are declared as "extension" => "mime type"
:
function get_allowed_mime_types() { static $mimes = false; if ( !$mimes ) { // Accepted MIME types are set here as PCRE unless provided. $mimes = apply_filters( 'upload_mimes', array( 'jpg|jpeg|jpe' => 'image/jpeg', 'gif' => 'image/gif', 'png' => 'image/png', 'bmp' => 'image/bmp', 'tif|tiff' => 'image/tiff', 'ico' => 'image/x-icon', 'asf|asx|wax|wmv|wmx' => 'video/asf', 'avi' => 'video/avi', 'divx' => 'video/divx', 'flv' => 'video/x-flv', 'mov|qt' => 'video/quicktime', 'mpeg|mpg|mpe' => 'video/mpeg', 'txt|asc|c|cc|h' => 'text/plain', 'csv' => 'text/csv', 'tsv' => 'text/tab-separated-values', 'rtx' => 'text/richtext', 'css' => 'text/css', 'htm|html' => 'text/html', 'mp3|m4a|m4b' => 'audio/mpeg', 'mp4|m4v' => 'video/mp4', 'ra|ram' => 'audio/x-realaudio', 'wav' => 'audio/wav', 'ogg|oga' => 'audio/ogg', 'ogv' => 'video/ogg', 'mid|midi' => 'audio/midi', 'wma' => 'audio/wma', 'mka' => 'audio/x-matroska', 'mkv' => 'video/x-matroska', 'rtf' => 'application/rtf', 'js' => 'application/javascript', 'pdf' => 'application/pdf', 'doc|docx' => 'application/msword', 'pot|pps|ppt|pptx|ppam|pptm|sldm|ppsm|potm' => 'application/vnd.ms-powerpoint', 'wri' => 'application/vnd.ms-write', 'xla|xls|xlsx|xlt|xlw|xlam|xlsb|xlsm|xltm' => 'application/vnd.ms-excel', 'mdb' => 'application/vnd.ms-access', 'mpp' => 'application/vnd.ms-project', 'docm|dotm' => 'application/vnd.ms-word', 'pptx|sldx|ppsx|potx' => 'application/vnd.openxmlformats-officedocument.presentationml', 'xlsx|xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml', 'docx|dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml', 'onetoc|onetoc2|onetmp|onepkg' => 'application/onenote', 'swf' => 'application/x-shockwave-flash', 'class' => 'application/java', 'tar' => 'application/x-tar', 'zip' => 'application/zip', 'gz|gzip' => 'application/x-gzip', 'exe' => 'application/x-msdownload', // openoffice formats 'odt' => 'application/vnd.oasis.opendocument.text', 'odp' => 'application/vnd.oasis.opendocument.presentation', 'ods' => 'application/vnd.oasis.opendocument.spreadsheet', 'odg' => 'application/vnd.oasis.opendocument.graphics', 'odc' => 'application/vnd.oasis.opendocument.chart', 'odb' => 'application/vnd.oasis.opendocument.database', 'odf' => 'application/vnd.oasis.opendocument.formula', // wordperfect formats 'wp|wpd' => 'application/wordperfect', ) ); } return $mimes; }
If you want to display all supported file types in your custom page (like theme option or plugin option), you can use the code below:
$mimes = get_allowed_mime_types(); $types = array(); foreach ($mimes as $ext => $mime) { $types[] = '<li>' . str_replace('|', ', ', $ext) . '</li>'; } echo '<ul>' . implode('', $types) . '</ul>';
Another note about the function above is that it uses a filter for file types. That means you can use a filter to add/remove allowed file types. Here's a sample code:
add_filter('upload_mimes', 'rw_alter_file_types', 1, 1); function rw_alter_file_types($mimes) { // add file type $mimes['phps'] = 'text/plain'; // remove file type unset($mimes['wav']); return $mimes; }
Hope that help you (a bit) when dealing with uploading in WordPress.
Leave a Reply