You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

wp_kses_allowed_html过滤器对部分SVG标签失效问题排查

解决ACF中SVG的linearGradient/clipPath标签被wp_kses过滤的问题

问题原因

wp_kses在处理HTML标签时,会自动将所有标签名转换为小写进行匹配。你代码里用的linearGradient和clipPath是驼峰命名,和wp_kses内部处理后的小写标签名不匹配,所以这两个标签被判定为不允许,直接被过滤掉了。

修复后的过滤器代码

把标签数组的键名改成全小写,同时注意属性名也要转为小写(wp_kses同样会统一处理属性名的大小写):

function acf_add_allowed_svg_tag( $tags, $context ) {
    if ( $context === 'acf' ) {
        $tags['svg']  = array(
            'xmlns'             => true,
            'width'         => true,
            'height'        => true,
            'preserveaspectratio'   => true,
            'fill'              => true,
            'viewbox'               => true,
            'role'              => true,
            'aria-hidden'           => true,
            'focusable'             => true,
        );
        $tags['path'] = array(
            'd'    => true,
            'fill' => true,
        );
        $tags['g'] = [
            'clip-path' => true
        ];
        // 改为全小写的标签名
        $tags['lineargradient'] = [
            'id' => true,
            'x1' => true,
            'x2' => true,
            'y1' => true,
            'y2' => true,
            'gradientunits' => true,
        ];
        $tags['clippath'] = [
            'id' => true,
        ];
        $tags['stop'] = [
            'stop-color' => true,
            'offset' => true
        ];
        // Others removed for brevity
    }
    return $tags;
}
add_filter( 'wp_kses_allowed_html', 'acf_add_allowed_svg_tag', 10, 2 );

验证效果

修改后,原本被过滤的linearGradient和clipPath标签会被正常保留,SVG的渐变和裁剪路径功能也能正常生效。


内容的提问来源于stack exchange,提问作者user2407400

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 05:55:05