admin管理员组文章数量:1287597
I tried hooking the upload_mimes
hook in order to add support for ttf files to be uploaded, however this didn't immediately work (.ttf files were still blocked).
The reason isn't entirely clear to me but I assume Wordpress's calculated MIME type for the file I'm trying to upload and the MIME type I'm adding to the upload_files
hook don't match.
I tried hooking the upload_mimes
hook in order to add support for ttf files to be uploaded, however this didn't immediately work (.ttf files were still blocked).
The reason isn't entirely clear to me but I assume Wordpress's calculated MIME type for the file I'm trying to upload and the MIME type I'm adding to the upload_files
hook don't match.
2 Answers
Reset to default 3This is an imperfect solution which forces the way Wordpress detects TTF files to use the file extension and then hooks upload_mimes
for exactly our specified MIME type. Part of this answer taken from https://diviengine/how-to-fix-sorry-this-file-type-is-not-permitted-for-security-reasons/
First massage the way Wordpress detects/reports TTF file types:
function divi_engine_font_correct_filetypes( $data, $file, $filename, $mimes, $real_mime ) {
if ( ! empty( $data['ext'] ) && ! empty( $data['type'] ) ) {
return $data;
}
$wp_file_type = wp_check_filetype( $filename, $mimes );
// Check for the file type you want to enable, e.g. 'svg'.
if ( 'ttf' === $wp_file_type['ext'] ) {
$data['ext'] = 'ttf';
$data['type'] = 'font/ttf';
}
if ( 'otf' === $wp_file_type['ext'] ) {
$data['ext'] = 'otf';
$data['type'] = 'font/otf';
}
return $data;
}
add_filter( 'wp_check_filetype_and_ext', 'divi_engine_font_correct_filetypes', 10, 5 );
Then hook upload_mimes
for exactly this extension/mime-type:
function allow_custom_mime_types( $mimes ) {
// New allowed mime types.
$mimes['ttf'] = 'font/ttf';
return $mimes;
}
add_filter( 'upload_mimes', 'allow_custom_mime_types' );
If you're not in production or i.e. working in development env you can use:
define('ALLOW_UNFILTERED_UPLOADS', true);
in wp-config.php
本文标签: How can I allow upload of ttf or otf font files when hooking uploadmimes doesn39t work
版权声明:本文标题:How can I allow upload of ttf or otf font files when hooking `upload_mimes` doesn't work 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741292376a2370624.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论