0

Trying to add certain allowed MIME types to the upload filter - currently my custom function looks like this:

// allow SVG and video mime types for upload
function cc_mime_types( $mimes ){
    $mimes['svg']  = 'image/svg+xml';
    $mimes['webm'] = 'video/webm';
    $mimes['mp4']  = ['video/mp4','video/mpeg'];
    $mimes['ogg']  = 'video/ogg';
    $mimes['ogv']  = 'video/ogg';
    return $mimes;
}
add_filter( 'upload_mimes', 'cc_mime_types' );

Uploading videos with ogg/ogv format seems to work well, but MP4 is failing. It seems like an array of different MIME types is not the solution. How should I add this?

1

This filter only accepts strings for mime types. It also already has support for webm, mp4, mpeg, ogv, and ogg.

I think you can remove everything except the line for SVGs.

A word of warning, WordPress doesn't support SVG upload by default because it's a security concern. Be careful about enabling this. At the least, use something like the Safe SVG plugin to help mitigate the risk.

| improve this answer | |
  • Not all files/extensions in this list are allowed within the upload_mime's filter (at least not in the installation I have tested it). Anyway thank you very much for the list (+1), now I know ho to do it. – Blackbam May 29 '19 at 16:11
1

@MikeNGarret's answer pointed me to the interesting function wp_get_mime_types(). Within this function you can look up how to correctly add MIME types to the upload_mimes filter (https://core.trac.wordpress.org/browser/tags/5.1.1/src/wp-includes/functions.php#L2707).

The correct answer therefore is:

// allow SVG and video mime types for upload
function cc_mime_types( $mimes ){
    $mimes['svg']      = 'image/svg+xml';
    $mimes['webm']     = 'video/webm';
    $mimes['mp4|m4v']  = 'video/mp4';
    $mimes['mpeg|mpg|mpe']  = 'video/mpeg';
    $mimes['ogv']      = 'video/ogg';
    return $mimes;
}
add_filter( 'upload_mimes', 'cc_mime_types' );

For registering an unknown MIME type you should therefore use the mime_types filter, for the upload (independently) you have to add it to the list of allowed upload mimes.

| improve this answer | |
  • Note that this answers half the question - you can add multiple extensions to a single mime-type. But how can you do the other half, add multiple mime-types to a single extension? eg a .DXF file can legitimately be text/plain or applications/octet-stream as both text and binary versions exist. – benz001 Apr 30 at 9:17
  • @benz001 Never have needed this. Have you tried an array? – Blackbam Apr 30 at 11:18
  • 1
    Yes, it didn't work, also tried comma-separated mimes as per some other article - what I had to do was wrap around the check and if it failed loop through an array of alternate mime types retrying the check each time as per wordpress.stackexchange.com/questions/323750/… This seems like a core bug that there is no filter/mechanism to support this - for us it killed our file upload gravity form. – benz001 May 2 at 9:11
  • @benz001 Ok thats good to know. You should do a feature request in: core.trac.wordpress.org - because this is really something WordPress should be capable of. – Blackbam May 4 at 8:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.