Skip to content

Early-bird lifetime licence: $199 once, for the first 200 buyers only. See the offer

Let administrators upload font files

Administrators can upload WOFF2, WOFF, TTF and OTF fonts to the media library, for use in your CSS.

  • Type PHP
  • Runs Everywhere (runs as WordPress loads)
  • Category Media
  • Tested with WordPress 7.1
PHP
<?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.

Move your snippets over this afternoon.

Importing changes nothing until you switch over, and your old shortcodes keep working.