我正在处理主题设置页面,我需要允许上载WOFF 和WOFF2 文件。获取
抱歉,出于安全原因,不允许使用此文件类型。
此时的消息。
我的代码:
function my_mime_types($mimes = array()) {
// $mimes[\'svg\'] = \'image/svg+xml\'; // insecure; do not use!
// Try https://wordpress.org/plugins/safe-svg/ if you need to upload SVGs
$mimes[\'webp\'] = \'image/webp\';
$mimes[\'ogg\'] = \'audio/ogg\';
$mimes[\'woff\'] = \'font/woff\';
$mimes[\'woff2\'] = \'font/woff2\';
return $mimes;
}
add_filter( \'upload_mimes\', \'my_mime_types\' );
我跑了
file --mime-type -b /path/to/fonts/font.woff
, 返回的
application/octet-stream
所以我更新了代码,但仍然没有成功。我也试过了
application/x-font-woff
未成功。
我还得到了一个错误.webp 文件mime类型匹配;运行确认mime-type
.
我知道最新的Wordpress版本采用了严格的mime类型检查。这可能是罪魁祸首吗?有什么解决办法吗?
更新:工作代码基于Ratan Maurya的回答:
add_filter( \'wp_check_filetype_and_ext\', \'my_file_and_ext_webp\', 10, 4 );
function my_file_and_ext_webp( $types, $file, $filename, $mimes ) {
if ( false !== strpos( $filename, \'.webp\' ) ) {
$types[\'ext\'] = \'webp\';
$types[\'type\'] = \'image/webp\';
}
if ( false !== strpos( $filename, \'.ogg\' ) ) {
$types[\'ext\'] = \'ogg\';
$types[\'type\'] = \'audio/ogg\';
}
if ( false !== strpos( $filename, \'.woff\' ) ) {
$types[\'ext\'] = \'woff\';
$types[\'type\'] = \'font/woff|application/font-woff|application/x-font-woff|application/octet-stream\';
}
if ( false !== strpos( $filename, \'.woff2\' ) ) {
$types[\'ext\'] = \'woff2\';
$types[\'type\'] = \'font/woff2|application/octet-stream|font/x-woff2\';
}
return $types;
}
function my_mime_types($mimes) {
// $mimes[\'svg\'] = \'image/svg+xml\'; // insecure; do not use! Try https://wordpress.org/plugins/safe-svg/ if you need to upload SVGs
$mimes[\'webp\'] = \'image/webp\';
$mimes[\'ogg\'] = \'audio/ogg\';
$mimes[\'woff\'] = \'font/woff|application/font-woff|application/x-font-woff|application/octet-stream\';
$mimes[\'woff2\'] = \'font/woff2|application/octet-stream|font/x-woff2\';
return $mimes;
}
add_filter( \'upload_mimes\', \'my_mime_types\' );