Let administrators upload font files
Administrators can upload WOFF2, WOFF, TTF and OTF fonts to the media library, for use in your CSS.
<?php
add_filter(
'upload_mimes',
static function ( $mimes ) {
if ( current_user_can( 'manage_options' ) ) {
$mimes['woff2'] = 'font/woff2';
$mimes['woff'] = 'font/woff';
$mimes['ttf'] = 'font/ttf';
$mimes['otf'] = 'font/otf';
}
return $mimes;
}
);
// PHP reports fonts under many types; trust the extension for administrators.
add_filter(
'wp_check_filetype_and_ext',
static function ( $data, $file, $filename, $mimes ) {
if ( ! empty( $data['ext'] ) || ! current_user_can( 'manage_options' ) ) {
return $data;
}
$check = wp_check_filetype( $filename, $mimes );
if ( in_array( $check['ext'], array( 'woff2', 'woff', 'ttf', 'otf' ), true ) ) {
$data['ext'] = $check['ext'];
$data['type'] = $check['type'];
}
return $data;
},
10,
4
);
Good to know
The Font Library in Appearance > Editor has its own upload for block themes.
In these packs
- Images and media: Sharper, lighter images, tidier uploads and a more useful media library.