Support » Developing with WordPress » upload_mimes filter not working correctly

  • So it appears that the upload_mimes filter is not working as expected. After much research and compared my code to multiple examples, I cannot figure out why it is not working. So in my code, I set what file types to accept and unset ALL AUDIO & VIDEO file types. But I can still upload our mp4 vidoes.

    add_filter( 'upload_mimes', 'my_mime_types', 1, 1 );
    if ( ! function_exists( 'my_mime_types' ) ) {
        function my_mime_types( $mime_types  ) {
            $mime_types['jpg'] = 'image/jpeg';
            $mime_types['png'] = 'image/png';
            $mime_types['pdf'] = 'application/pdf';
            $mime_types['doc'] = 'application/msword';
            $mime_types['docx'] = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
            $mime_types['kmz'] = 'application/vnd.google-earth.kml+xml';
    
            unset($mime_types['mp3']);
            unset($mime_types['m4a']);
            unset($mime_types['wav']);
            unset($mime_types['ogg']);
            unset($mime_types['mp4']);
            unset($mime_types['mov']);
            unset($mime_types['wmv']);
            unset($mime_types['avi']);
            unset($mime_types['mpg']);
            unset($mime_types['ogv']);
            unset($mime_types['3gp']);
            unset($mime_types['3g2']);
            return $mime_types;
        }
    }

    Thanks!

Viewing 6 replies - 1 through 6 (of 6 total)
  • It looks correct. Did you try it with a higher number for priority, in case other filters are adding those types?

    Thread Starter dxladner

    (@dxladner)

    Thanks for the response Joy, I changed priority to 99 nothing then to 9999 and still it lets videos upload.

    Anyone else run into these issue?

    Thanks!

    Moderator bcworkz

    (@bcworkz)

    The MIME type restrictions only apply to users without the “unfiltered_upload” capability. You can remove it from any role or individual users if you want. The code to do so isn’t too complicated, or use one of the roles and capabilities plugins. Changes are persistent, so don’t use code that runs on every request.

    Thread Starter dxladner

    (@dxladner)

    @bcworkz thanks for the info (the “unfiltered_upload” cap), I will give the Role plugin a try since we already have it installed.

    Thread Starter dxladner

    (@dxladner)

    @bcworkz So we are using the User Role Editor, so he is my issue, since there are 3 of us that are admin, I want to limit EVERYONE even me from uploading any media types. So in the User Role Editor, Admin role is not able to be adjusted. Any other suggestion to block any media types as being able to be uploaded?

    Moderator bcworkz

    (@bcworkz)

    Can you create a new “Super Editor” role or something that has all capabilities except for the unfiltered_upload cap? Switch everyone to the new role and stop using administrator. There does need to be one administrator account, but no one has to actively use it.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘upload_mimes filter not working correctly’ is closed to new replies.