
在woocommerce中自定义邮件模板,尤其是需要插入特定格式的文本或动态数据时,可能会遇到一些挑战。开发者通常会直接修改主题或插件提供的邮件模板文件,使用php代码来控制输出。然而,这涉及到wordpress的安全机制,特别是wp_kses函数,它用于过滤html以防止跨站脚本攻击(xss)。
例如,当尝试在邮件中实现以下布局时:
原始的PHP尝试可能如下所示:
<?php if ( $order->needs_payment() ) { ?>
<p>
<?php
printf(
wp_kses(
/* translators: %1$s Site title, %2$s Order pay link */
__( 'We’re delighted to let you know that the first print of <i>The Versatile Home</i> is now available and we are able to fulfil your pre-order. Your invoice is below and here is a link to make payment: %2$s', 'woocommerce' ),
array(
'a' => array(
'href' => array(),
),
)
),
esc_html( get_bloginfo( 'name', 'display' ) ),
'<a href="' . esc_url( $order->get_checkout_payment_url() ) . '">' . esc_html__( 'click here to pay by credit/debit card or PayPal', 'woocommerce' ) . '</a>'
);
?>
</p>
<?php } ?>在这个示例中,<i>标签未能正确渲染斜体效果,并且第二段内容及其中的动态订单号也无法直接添加。这主要是因为wp_kses函数默认或根据提供的白名单,移除了未被允许的HTML标签。
要使用纯PHP代码实现上述需求,需要对wp_kses的工作原理有深入理解,并掌握如何正确地构建和拼接HTML字符串。
wp_kses函数接受一个允许的HTML标签及其属性的数组作为第二个参数。如果需要显示<i>标签,必须将其明确添加到允许列表中。
示例代码:
<?php if ( $order->needs_payment() ) { ?>
<p>
<?php
printf(
wp_kses(
__( 'We’re delighted to let you know that the first print of <i>The Versatile Home</i> is now available and we are able to fulfil your pre-order. Your invoice is below and here is a link to make payment: %2$s', 'woocommerce' ),
array(
'a' => array(
'href' => array(),
),
'i' => array(), // 明确允许 <i> 标签
)
),
esc_html( get_bloginfo( 'name', 'display' ) ),
'<a href="' . esc_url( $order->get_checkout_payment_url() ) . '">' . esc_html__( 'click here to pay by credit/debit card or PayPal', 'woocommerce' ) . '</a>'
);
?>
</p>
<?php } ?>通过在wp_kses的允许标签数组中添加'i' => array(),<i>标签将不再被过滤,从而实现斜体效果。
要添加第二个段落,通常需要一个新的printf或echo语句块。动态订单号可以通过$order->get_id()方法获取。
示例代码:
<?php if ( $order->needs_payment() ) { ?>
<p>
<?php
// 第一段内容,包含斜体和支付链接
printf(
wp_kses(
__( 'We’re delighted to let you know that the first print of <i>The Versatile Home</i> is now available and we are able to fulfil your pre-order. Your invoice is below and here is a link to make payment: %2$s', 'woocommerce' ),
array(
'a' => array( 'href' => array(), ),
'i' => array(), // 允许 <i> 标签
)
),
esc_html( get_bloginfo( 'name', 'display' ) ),
'<a href="' . esc_url( $order->get_checkout_payment_url() ) . '">' . esc_html__( 'click here to pay by credit/debit card or PayPal', 'woocommerce' ) . '</a>'
);
?>
</p>
<p>
<?php
// 第二段内容,包含银行转账信息和动态订单号
printf(
wp_kses(
__( 'Alternatively, you can make payment by bank transfer to Sort Code: 00-00-00, Acct #: 00000000, Acct name: xxxxx, quoting order # %s as the reference. As soon as we’ve received the payment we’ll get your order off to you straight away.', 'woocommerce' ),
array() // 此段落如果不需要额外HTML标签,可以留空或使用 wp_kses_post
),
esc_html( $order->get_id() ) // 获取并安全输出订单号
);
?>
</p>
<?php } ?>注意事项:
鉴于直接使用PHP代码进行WooCommerce邮件模板的复杂性、维护难度和对安全性的高要求,许多开发者和商家倾向于使用专业的邮件构建器插件。这些插件通常提供可视化的拖放界面,极大地简化了邮件模板的设计和自定义过程。
以YayMail为例,这类插件的优势包括:
使用YayMail这类工具,上述的斜体文本和包含动态订单号的新段落,都可以通过简单的文本编辑和拖放操作来完成,而无需深入理解PHP代码和wp_kses的细节。这对于非开发人员或希望提高效率的开发人员来说,无疑是更“轻松”的选择。
在WooCommerce邮件自定义中,虽然可以通过直接修改PHP代码来实现复杂的文本格式和动态内容,但这要求开发者具备扎实的PHP和WordPress安全编码知识,且维护成本较高。对于大多数场景,尤其是追求效率和便捷性的用户,使用专业的邮件构建器(如YayMail)是更为推荐的解决方案。它不仅简化了邮件设计过程,提高了效率,还能确保邮件内容的美观性和功能性,让您能够专注于业务本身,而非繁琐的代码调试。
以上就是WooCommerce邮件自定义:文本格式与动态内容插入的挑战与解决方案的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号