Media PHP Intermediate

Allow Extra File Types in the Media Library

Let administrators upload file types WordPress blocks by default, such as subtitle files (.vtt), JSON or 3D models (.glb).

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 28 lines
add_filter( 'upload_mimes', function ( array $mimes ): array {
	if ( ! current_user_can( 'manage_options' ) ) {
		return $mimes;
	}

	$mimes['vtt']  = 'text/vtt';
	$mimes['json'] = 'application/json';
	$mimes['glb']  = 'model/gltf-binary';

	return $mimes;
} );

// WordPress double-checks the real file type. Tell it these are fine.
add_filter( 'wp_check_filetype_and_ext', function ( array $data, string $file, string $filename, $mimes ): array {
	if ( ! empty( $data['ext'] ) || ! current_user_can( 'manage_options' ) ) {
		return $data;
	}

	$allowed = [ 'vtt' => 'text/vtt', 'json' => 'application/json', 'glb' => 'model/gltf-binary' ];
	$ext     = strtolower( pathinfo( $filename, PATHINFO_EXTENSION ) );

	if ( isset( $allowed[ $ext ] ) ) {
		$data['ext']  = $ext;
		$data['type'] = $allowed[ $ext ];
	}

	return $data;
}, 10, 4 );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.

WordPress only accepts a fixed list of file types. Uploading a subtitle file for a video, or a JSON or 3D model file, gives “Sorry, you are not allowed to upload this file type”. This snippet allows a few more, for administrators only.

How it works

upload_mimes is the list of allowed extensions and their MIME types. The snippet adds three for administrators.

WordPress also checks the actual contents of each file. For plain text formats like VTT and JSON, that check often can’t confirm the type, so wp_check_filetype_and_ext fills in the type for these extensions when WordPress couldn’t.

Things to watch

  • Only allow types you need. Never allow executable types like .php, .phtml or .exe, or HTML files.
  • For SVG, use “Allow SVG Uploads Safely”, which also cleans the file.

How to test it

Log in as an administrator and upload a .vtt file. It should be added to the Media Library.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only