WordPress 和 SVG

Ric*_*son 5 wordpress svg

有谁知道如何让 WordPress 允许使用通过主题定制器面板上传的 SVG 文件?

在主题functions.php文件中添加了以下内容,该文件允许上传SVG(尽管没有预览或特色图像)。

function custom_mtypes( $m ){
    $m['svg'] = 'image/svg+xml';
    $m['svgz'] = 'image/svg+xml';
    return $m;
}
add_filter( 'upload_mimes', 'custom_mtypes' );
Run Code Online (Sandbox Code Playgroud)

然而,这仍然不允许我从文件系统上传或选择 SVG 或拖放 SVG。

小智 7

更新(Wordpres 5+)

确保每个 SVG 文件以以下内容开头:

<?xml version="1.0" encoding="utf-8"?>
Run Code Online (Sandbox Code Playgroud)


Ale*_*_TT 5

如果您\xe2\x80\x99尝试过通过媒体上传器将 SVG 上传到 WordPress,则可能会遇到一些问题。

\n\n

它要么给您一个错误,并且不允许您上传文件,要么允许您上传 .svg 文件,但只是不会显示它。不管怎样,这里\xe2\x80\x99有两个简单的步骤,可以轻松地在 WordPress 中启用 SVG 图像。

\n\n

注意:您\xe2\x80\x99s 需要能够编辑主题\xe2\x80\x99s(或子主题\xe2\x80\x99s)functions.php文件和根.htaccess文件才能正常工作。

\n\n

将 SVG MIME 类型添加到functions.php

\n\n
function wpcontent_svg_mime_type( $mimes = array() ) {\n  $mimes[\'svg\']  = \'image/svg+xml\';\n  $mimes[\'svgz\'] = \'image/svg+xml\';\n  return $mimes;\n}\nadd_filter( \'upload_mimes\', \'wpcontent_svg_mime_type\' );   \n
Run Code Online (Sandbox Code Playgroud)\n\n

您应该替换wpcontent_为您自己的名称空间。该函数只是将 SVG 和 SVGZ(压缩 SVG)添加到 WordPress 中允许的上传文件类型中,并挂钩到 upload_mimes() WordPress 函数。

\n\n

将 SVG Mime 类型添加到 .htaccess

\n\n

因此,打开根.htaccess文件并在#End WordPress.

\n\n
# Add SVG Mime Types\nAddType image/svg+xml svg\nAddType image/svg+xml svgz       \n
Run Code Online (Sandbox Code Playgroud)\n\n

保存文件,\xe2\x80\x99 就完成了!您现在可以从 Illustrator 或 Inkscape 保存 SVG 并在 WordPress 网站上使用它们。

\n\n

来源在这里

\n


小智 3

您可以通过将其添加到当前主题的functions.php 文件中来克服安全警告。

add_filter('upload_mimes', 'custom_upload_mimes');

function custom_upload_mimes ( $existing_mimes=array() ) {
    // add the file extension to the array
    $existing_mimes['svg'] = 'mime/type';
        // call the modified list of extensions
    return $existing_mimes;
}
Run Code Online (Sandbox Code Playgroud)

然后您应该能够上传扩展名为 .svg 的文件