我正在处理一个主题设置页面,我需要允许上传WOFF和<>WOFF 2文件。弄到
对不起,出于安全原因,不允许使用此文件类型。
此时留言。
我的代码:
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类型检查。那会是罪魁祸首吗?有解决办法吗?
更新:基于Ram 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' );发布于 2019-05-27 04:44:43
我在https://wordpress.stackexchange.com/a/323226/24756看到了一个正确的实现。从技术上讲,您还需要向下传递扩展以过滤wp_check_filetype_and_ext。
对于WOFF/WOFF 2,我建议使用以下格式:
$mimes['woff'] = 'font/woff|application/font-woff|application/x-font-woff|application/octet-stream';
$mimes['woff2'] = 'font/woff2|application/octet-stream|font/x-woff2';对于.webp来说,它可以是简单的image/webp。
希望能帮上忙。
https://wordpress.stackexchange.com/questions/338624
复制相似问题