Skip to main content

Disallow Video Mime Types

PropertyValue
descriptionDisallow common video MIME types from Media Library uploads.
tagslib, php, wp, oop
rating

Overview

Disallow common video file types from WordPress media uploads.

The snippet removes MIME mappings for mp4, mov, webm, avi, mkv, wmv, and m4v through the upload_mimes filter. Use it when video files should live in a dedicated video host or storage workflow instead of the Media Library.

Variants

<?php
/**
* Disallow common video MIME types from uploads.
*
* @param array<string,string> $mimes Allowed MIME types.
* @return array<string,string>
*/
if ( ! function_exists( 'mac_disallow_video_mime_types' ) ) {

function mac_disallow_video_mime_types( array $mimes ): array {

/** @var string[] $blocked_extensions */
$blocked_extensions = [
'mp4',
'mov',
'webm',
'avi',
'mkv',
'wmv',
'm4v',
];

foreach ( array_keys( $mimes ) as $key ) {

$extensions = explode( '|', strtolower( (string) $key ) );

if ( array_intersect( $extensions, $blocked_extensions ) ) {
unset( $mimes[ $key ] );
}
}

return $mimes;
}
}

add_filter( 'upload_mimes', 'mac_disallow_video_mime_types' );