Skip to main content

Allow Font Mime Types

PropertyValue
descriptionAllow common font MIME types for uploads.
tagslib, php, wp, oop
rating

Overview

Allow common font file types in WordPress media uploads.

The snippet adds MIME mappings for ttf, otf, woff, woff2, and eot through the upload_mimes filter. Use it when a site needs to upload custom fonts through the Media Library or a builder's font upload UI.

Variants

<?php
/**
* Allow common font MIME types for uploads.
*
* @param array<string,string> $mimes Allowed MIME types.
* @return array<string,string>
*/
if ( ! function_exists( 'mac_allow_font_mime_types' ) ) {

function mac_allow_font_mime_types( array $mimes ): array {

/** @var array<string,string> $font_mimes */
$font_mimes = [
'ttf' => 'font/sfnt',
'otf' => 'font/otf',
'woff' => 'font/woff',
'woff2' => 'font/woff2',
'eot' => 'application/vnd.ms-fontobject',
];

foreach ( $font_mimes as $ext => $mime ) {
$mimes[ $ext ] = $mime;
}

return $mimes;
}
}

add_filter( 'upload_mimes', 'mac_allow_font_mime_types' );