WordPress 解决无法上传 WebP、ico、svg附件

WebP是Google开发的一种图像格式(就像png和jpg一样)。WebP格式(.webp)的图像通常要小得多,这可以使网站更快并且使用更少的带宽。

根据图像的不同,尺寸可能会缩小25%至70%。

当你上传不受支持的图片格式时,就会出现以下错误提示:

Webp上传错误

WordPress添加上传文件类型的的方法:

只需要添加以下代码到您的主题下的 functions.php 文件。

这里我们添加了对svg、ico、webp的支持;

add_filter('upload_mimes', 'tbi_upload_mimes');
function tbi_upload_mimes($mimes = array()) {
    $mimes['svg'] = 'image/svg+xml';
    $mimes['ico'] = 'image/x-icon';
    $mimes['webp'] = 'image/webp';
    return $mimes;
}

我们(推荐)使用 Woody Snippets 插件管理您的php代码,避免更换、升级主题被官方文件替换。

没事最好别随便修改

如果你还想添加其它类型附件,可以参看下面 MIME 类型添加;

    $mimes['jpg|jpeg|jpe'] = 'image/jpeg'
    $mimes['gif'] = 'image/gif'
    $mimes['png'] = 'image/png'
    $mimes['bmp'] = 'image/bmp'
    $mimes['tiff|tif'] = 'image/tiff'
    $mimes['ico'] = 'image/x-icon'
    $mimes['asf|asx'] = 'video/x-ms-asf'
    $mimes['wmv'] = 'video/x-ms-wmv'
    $mimes['wmx'] = 'video/x-ms-wmx'
    $mimes['wm'] = 'video/x-ms-wm'
    $mimes['avi'] = 'video/avi'
    $mimes['divx'] = 'video/divx'
    $mimes['flv'] = 'video/x-flv'
    $mimes['mov|qt'] = 'video/quicktime'
    $mimes['mpeg|mpg|mpe'] = 'video/mpeg'
    $mimes['mp4|m4v'] = 'video/mp4'
    $mimes['ogv'] = 'video/ogg'
    $mimes['webm'] = 'video/webm'
    $mimes['mkv'] = 'video/x-matroska'
    $mimes['3gp|3gpp'] = 'video/3gpp'
    $mimes['3g2|3gp2'] = 'video/3gpp2'
    $mimes['txt|asc|c|cc|h|srt'] = 'text/plain'
    $mimes['csv'] = 'text/csv'
    $mimes['tsv'] = 'text/tab-separated-values'
    $mimes['ics'] = 'text/calendar'
    $mimes['rtx'] = 'text/richtext'
    $mimes['css'] = 'text/css'
    $mimes['htm|html'] = 'text/html'
    $mimes['vtt'] = 'text/vtt'
    $mimes['dfxp'] = 'application/ttaf+xml'
    $mimes['mp3|m4a|m4b'] = 'audio/mpeg'
    $mimes['aac'] = 'audio/aac'
    $mimes['ra|ram'] = 'audio/x-realaudio'
    $mimes['wav'] = 'audio/wav'
    $mimes['ogg|oga'] = 'audio/ogg'
    $mimes['flac'] = 'audio/flac'
    $mimes['mid|midi'] = 'audio/midi'
    $mimes['wma'] = 'audio/x-ms-wma'
    $mimes['wax'] = 'audio/x-ms-wax'
    $mimes['mka'] = 'audio/x-matroska'
    $mimes['rtf'] = 'application/rtf'
    $mimes['js'] = 'application/javascript'
    $mimes['pdf'] = 'application/pdf'
    $mimes['class'] = 'application/java'
    $mimes['tar'] = 'application/x-tar'
    $mimes['zip'] = 'application/zip'
    $mimes['gz|gzip'] = 'application/x-gzip'
    $mimes['rar'] = 'application/rar'
    $mimes['7z'] = 'application/x-7z-compressed'
    $mimes['psd'] = 'application/octet-stream'
    $mimes['xcf'] = 'application/octet-stream'
    $mimes['doc'] = 'application/msword'
    $mimes['pot|pps|ppt'] = 'application/vnd.ms-powerpoint'
    $mimes['wri'] = 'application/vnd.ms-write'
    $mimes['xla|xls|xlt|xlw'] = 'application/vnd.ms-excel'
    $mimes['mdb'] = 'application/vnd.ms-access'
    $mimes['mpp'] = 'application/vnd.ms-project'
    $mimes['docx'] = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
    $mimes['docm'] = 'application/vnd.ms-word.document.macroEnabled.12'
    $mimes['dotx'] = 'application/vnd.openxmlformats-officedocument.wordprocessingml.template'
    $mimes['dotm'] = 'application/vnd.ms-word.template.macroEnabled.12'
    $mimes['xlsx'] = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
    $mimes['xlsm'] = 'application/vnd.ms-excel.sheet.macroEnabled.12'
    $mimes['xlsb'] = 'application/vnd.ms-excel.sheet.binary.macroEnabled.12'
    $mimes['xltx'] = 'application/vnd.openxmlformats-officedocument.spreadsheetml.template'
    $mimes['xltm'] = 'application/vnd.ms-excel.template.macroEnabled.12'
    $mimes['xlam'] = 'application/vnd.ms-excel.addin.macroEnabled.12'
    $mimes['pptx'] = 'application/vnd.openxmlformats-officedocument.presentationml.presentation'
    $mimes['pptm'] = 'application/vnd.ms-powerpoint.presentation.macroEnabled.12'
    $mimes['ppsx'] = 'application/vnd.openxmlformats-officedocument.presentationml.slideshow'
    $mimes['ppsm'] = 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12'
    $mimes['potx'] = 'application/vnd.openxmlformats-officedocument.presentationml.template'
    $mimes['potm'] = 'application/vnd.ms-powerpoint.template.macroEnabled.12'
    $mimes['ppam'] = 'application/vnd.ms-powerpoint.addin.macroEnabled.12'
    $mimes['sldx'] = 'application/vnd.openxmlformats-officedocument.presentationml.slide'
    $mimes['sldm'] = 'application/vnd.ms-powerpoint.slide.macroEnabled.12'
    $mimes['onetoc|onetoc2|onetmp|onepkg'] = 'application/onenote'
    $mimes['oxps'] = 'application/oxps'
    $mimes['xps'] = 'application/vnd.ms-xpsdocument'
    $mimes['odt'] = 'application/vnd.oasis.opendocument.text'
    $mimes['odp'] = 'application/vnd.oasis.opendocument.presentation'
    $mimes['ods'] = 'application/vnd.oasis.opendocument.spreadsheet'
    $mimes['odg'] = 'application/vnd.oasis.opendocument.graphics'
    $mimes['odc'] = 'application/vnd.oasis.opendocument.chart'
    $mimes['odb'] = 'application/vnd.oasis.opendocument.database'
    $mimes['odf'] = 'application/vnd.oasis.opendocument.formula'
    $mimes['wp|wpd'] = 'application/wordperfect'
    $mimes['key'] = 'application/vnd.apple.keynote'
    $mimes['numbers'] = 'application/vnd.apple.numbers'
    $mimes['pages'] = 'application/vnd.apple.pages'

发表评论

您的邮箱地址不会被公开。 必填项已用 * 标注