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

WordPress Schema中德语特殊字符被转为数字编码问题如何解决?

问题原因

你代码中使用的utf8_decode()函数是导致德语特殊字符异常的核心原因:

  • WordPress自定义字段默认使用UTF-8编码存储内容,无需额外转码
  • utf8_decode()的作用是将UTF-8字符串转换为ISO-8859-1(Latin-1)编码,该编码不支持ü、ä、ö等德语特殊字符以及长破折号等扩展字符,转码后就会出现数字编码形式的乱码
解决方法

直接移除两处utf8_decode()调用即可,修正后的代码如下:

add_filter( 'woocommerce_structured_data_product', 'filter__woocommerce_structured_data_product', 20, 2 );
function filter__woocommerce_structured_data_product( $schema, $product ) {
    global $post;
    $shipping_val = get_post_meta( $post->ID, 'pro_shipping', true ); 
    // 现在正常显示: CHF 10.–
    $colour_val = get_post_meta( $post->ID, 'pro_color', true );
    // 现在正常显示: Aussen: rosa und blau (metallisch), Bordüre: grün, Innen: lila
    $schema['shipping'] = array(
        '@type'  => 'Shipping',
        'name'   => $shipping_val ,
    );
    $schema['colour'] = array(
        '@type'  => 'Colour',
        'name'   => $colour_val ,
    );
    return $schema;
}

修改完成后清理站点缓存,重新查看页面中的Schema结构化数据,特殊字符即可正常展示。

补充说明

如果你的自定义字段是从非UTF-8编码的第三方系统同步而来,确实需要转码的话,请使用mb_convert_encoding()函数处理,避免使用PHP 8.2及以上版本已被废弃的utf8_decode()函数,示例:
$shipping_val = mb_convert_encoding(get_post_meta( $post->ID, 'pro_shipping', true ), 'UTF-8', '你的原编码');
把你的原编码替换为实际的源数据编码即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.23 22:45:01