解决WordPress无法上传SVG等格式文件

今天要传个SVG格式图片,才发现WordPress不支持SVG格式文件的上传。提示抱歉,出于安全的考虑,不支持此文件类型。

百度了一下解决方法,做个笔记。

解决办法

方法一:

将下面的代码放进主题文件夹functions.php中。若要支持其他格式的文件(如rar等),参照第二行代码增加就行~

function my_upload_mimes($mimes = array()) {
    $mimes['svg'] = 'image/svg+xml';

    $mimes['ico'] = 'image/x-icon';
    $mimes['webp'] = 'image/webp';
    return $mimes;
}
add_filter('upload_mimes', 'my_upload_mimes');

当然也可以在wp-includes/functions.php中找到wp_get_mime_types()函数,在其中按照对应格式添加即可。

如果你还想添加其它类型附件,可以参看下面 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'

关于SVG

SVG可缩放矢量图形(Scalable Vector Graphics)是基于可扩展标记语言(XML),用于描述二维矢量图形的一种图形格式。SVG是W3C(“World Wide Web ConSortium”即”国际互联网标准组织”)在2000年8月制定的一种新的二维矢量图形格式,也是规范中的网络矢量图形标准。SVG严格遵从XML语法,并用文本格式的描述性语言来描述图像内容,因此是一种和图像分辨率无关的矢量图形格式。

发表评论

Crypto logo

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus cursus rutrum est nec suscipit. Ut et ultrices nisi. Vivamus id nisl ligula. Nulla sed iaculis ipsum.

Contact

Company Name

Address