3

we can assign extra file-types, so, WP allows them to be uploaded, like:

add_filter('mime_types', 'my_mimes');
function my_mimes($all)
{
    $all['zip'] = 'application/zip';
    return $all;
}

But I have a question: How to assign multiple file types to 1 extension? For example, zip extension - some zip files have application/zip mime, while others have application/x-zip (like, coming from 7zip).

So, how can we assign both mimes to zip extension?

3

Note the stricter mime type check since WP 5.0.1 where the file content and file extension must match. See e.g. this recent question on the vtt file type.

Secondary mime type for a given file extension

Here's a suggestion how to support a secondary mime type for a given file extension. Let's take .vtt as an example. The core assumes the mime type of text/vtt for that file extension, but the real mime type from finfo_file() can sometimes be text/plain. The finfo_file() seems to be somewhat buggy. We can add a support for it as a secondary mime type with:

/**
 * Support for 'text/plain' as the secondary mime type of .vtt files,
 * in addition to the default 'text/vtt' support.
 */
add_filter( 'wp_check_filetype_and_ext', 'wpse323750_secondary_mime', 99, 4 );    

function wpse323750_secondary_mime( $check, $file, $filename, $mimes ) {
    if ( empty( $check['ext'] ) && empty( $check['type'] ) ) {
        // Adjust to your needs!
        $secondary_mime = [ 'vtt' => 'text/plain' ];

        // Run another check, but only for our secondary mime and not on core mime types.
        remove_filter( 'wp_check_filetype_and_ext', 'wpse323750_secondary_mime', 99, 4 );
        $check = wp_check_filetype_and_ext( $file, $filename, $secondary_mime );
        add_filter( 'wp_check_filetype_and_ext', 'wpse323750_secondary_mime', 99, 4 );
    }
    return $check;
}

Here we use the wp_check_filetype_and_ext filter to see if the check failed. In that case we run wp_check_filetype_and_ext() again but now only on our secondary mime type, disabling our filter callback in the meanwhile to avoid an infinite loop.

Multiple mime types for a given file extension

If we need to support more than two mime types for the .vtt files, then we can expand the above snippet with:

/**
 * Demo: Support for 'text/foo' and 'text/bar' mime types of .vtt files,
 * in addition to the default 'text/vtt' support.
 */
add_filter( 'wp_check_filetype_and_ext', 'wpse323750_multi_mimes', 99, 4 );

function wpse323750_multi_mimes( $check, $file, $filename, $mimes ) {
    if ( empty( $check['ext'] ) && empty( $check['type'] ) ) {
        // Adjust to your needs!
        $multi_mimes = [ [ 'vtt' => 'text/foo' ], [ 'vtt' => 'text/bar' ] ];

        // Run new checks for our custom mime types and not on core mime types.
        foreach( $multi_mimes as $mime ) {
            remove_filter( 'wp_check_filetype_and_ext', 'wpse323750_multi_mimes', 99, 4 );
            $check = wp_check_filetype_and_ext( $file, $filename, $mime );
            add_filter( 'wp_check_filetype_and_ext', 'wpse323750_multi_mimes', 99, 4 );
            if ( ! empty( $check['ext'] ) ||  ! empty( $check['type'] ) ) {
                return $check;
            }
        }
    }
    return $check;
}

I hope you can test it further and adjust it to your needs.

  • sorry, cant fully understand, is it ok to define like this - pastebin.com/raw/wRZbxqPU ? – T.Todua Dec 23 '18 at 15:19
  • 1
    Will have a better look when i get back online – birgire Dec 23 '18 at 15:45
  • 1
    @birgire hmm, I though this question works the other way - one extension, multiple mime types... – Krzysiek Dróżdż Dec 23 '18 at 17:11
  • 1
    Uff, I was starting to think that I’m loosing my mind because of that xmass stuff ;) – Krzysiek Dróżdż Dec 24 '18 at 0:31
  • 1
    @T.Todua I posted a suggestion that you can hopefully work with. Since I already had the .vtt file to test I used it in the example. I hope you don't mind :-) – birgire Dec 24 '18 at 0:31

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.