让WordPress支持上传SVG图片的方法与插件Safe SVG

追格官方小助手/ 2022年02月26日/ WordPress/ 浏览 1562

WordPress默认情况下不支持上传SVG,这主要是出于安全考虑,那么我们怎么让WordPress支持上传sv图片呢。


方法1:

WordPress后台 - 插件中搜索:Safe SVG - 安装启用,当然也可以下方链接中下载,并传到插件目录中。

WordPress官网下载:https://cn.wordpress.org/plugins/safe-svg/


方法2:

将下面代码添加到当前WordPress模板函数functions.php中即可。


add_filter('wp_check_filetype_and_ext', function ($data, $file, $filename, $mimes)
{
    global $wp_version;
 
    if ($wp_version !== '4.7.1') {
        return $data;
    }
 
    $filetype = wp_check_filetype($filename, $mimes);
 
    return [
        'ext'             => $filetype[ 'ext' ],
        'type'            => $filetype[ 'type' ],
        'proper_filename' => $data[ 'proper_filename' ],
    ];
 
}, 10, 4);
 
add_filter('upload_mimes', function ($mimes)
{
    $mimes[ 'svg' ] = 'image/svg+xml';
 
    return $mimes;
});

add_action('admin_head', function ()
{
    echo '<style>
        .attachment-266x266, .thumbnail img {
             width: 100% !important;
             height: auto !important;
        }
        </style>';
});


发表评论

暂无评论,抢个沙发...

客服 工单