Last active
June 16, 2020 16:41
-
-
Save ethanclevenger91/559ca1b162c3e357707297aba3e78c3c to your computer and use it in GitHub Desktop.
Custom Fonts otf/ttf mime rewrite
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo { | |
public function __construct() { | |
// ... | |
add_filter( 'wp_check_filetype_and_ext', array( $this, 'update_mime_types' ), 10, 3 ); | |
add_filter( 'upload_mimes', array( $this, 'add_fonts_to_allowed_mimes' ) ); | |
} | |
// ... | |
/** | |
* Allowed mime types and file extensions | |
* | |
* @since 1.0.0 | |
* @param array $mimes Current array of mime types. | |
* @return array $mimes Updated array of mime types. | |
*/ | |
public function add_fonts_to_allowed_mimes( $mimes ) { | |
$mimes['woff'] = 'application/x-font-woff'; | |
$mimes['woff2'] = 'application/x-font-woff2'; | |
$mimes['ttf'] = 'application/x-font-ttf'; | |
$mimes['svg'] = 'image/svg+xml'; | |
$mimes['eot'] = 'application/vnd.ms-fontobject'; | |
$mimes['otf'] = 'font/otf'; | |
return $mimes; | |
} | |
/** | |
* Correct the mome types and extension for the font types. | |
* | |
* @param array $defaults File data array containing 'ext', 'type', and | |
* 'proper_filename' keys. | |
* @param string $file Full path to the file. | |
* @param string $filename The name of the file (may differ from $file due to | |
* $file being in a tmp directory). | |
* @return Array File data array containing 'ext', 'type', and | |
*/ | |
public function update_mime_types( $defaults, $file, $filename ) { | |
if ( 'ttf' === pathinfo( $filename, PATHINFO_EXTENSION ) ) { | |
$defaults['type'] = 'application/x-font-ttf'; | |
$defaults['ext'] = 'ttf'; | |
} | |
if ( 'otf' === pathinfo( $filename, PATHINFO_EXTENSION ) ) { | |
$defaults['type'] = 'application/x-font-otf'; | |
$defaults['ext'] = 'otf'; | |
} | |
return $defaults; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment