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

WordPress/WooCommerce过滤器添加后HTML标签不生效问题

问题原因及解决方案

原因

WooCommerce在渲染订单完成提示语时,会对woocommerce_thankyou_order_received_text过滤器返回的内容执行HTML转义(比如通过esc_html()函数),导致你添加的<strong>等HTML标签被转换成实体字符(如<变为&lt;),无法被浏览器解析为格式标签,只能原样显示。

另外你的原函数存在小问题:当$order不存在时未返回原文本,可能引发意外错误,需要补充。

解决方案

方案一:改用动作钩子直接输出(推荐)

通过woocommerce_thankyou动作钩子直接自定义输出内容,绕过转义逻辑:

add_action( 'woocommerce_thankyou', 'cust_thankyou_text', 20, 1 );
function cust_thankyou_text( $order_id ){
    $order = wc_get_order( $order_id );
    if( $order ) {
        // 先移除默认的提示语输出
        remove_action( 'woocommerce_thankyou', 'woocommerce_order_details_table', 10 );
        // 输出自定义内容,包含HTML标签
        echo '<p class="woocommerce-notice woocommerce-notice--success woocommerce-thankyou-order-received">' 
             . esc_html__( 'Thank you. Your order has been received.', 'woocommerce' ) 
             . ' <strong>For the delivery please</strong></p>';
        // 重新添加订单详情表
        add_action( 'woocommerce_thankyou', 'woocommerce_order_details_table', 10 );
    }
}

方案二:保留过滤器并允许安全HTML

使用wp_kses_post()函数过滤自定义文本,允许安全的HTML标签通过,同时修复原函数的返回逻辑:

add_filter( 'woocommerce_thankyou_order_received_text', 'cust_thankyou_text', 20, 2 );
function cust_thankyou_text( $original_thankyou_text, $order ){
    if( $order ) {
        $custom_text = ' <strong>For the delivery please</strong>';
        // 允许安全的HTML标签,避免被转义
        return $original_thankyou_text . wp_kses_post( $custom_text );
    }
    // 没有订单时返回原文本
    return $original_thankyou_text;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 23:56:08