php - 如何避免 WooCommerce 电子邮件模板中的样式注入
问题描述
模板中有一些 HTML 元素,例如第 41 行的 templates/emails/email-header.phptable#template_container
,它们没有内联样式:
<table border="0" cellpadding="0" cellspacing="0" width="600" id="template_container">
但是当它在前端渲染时,它会出现很多内联样式:
<table border="0" cellpadding="0" cellspacing="0" width="600" id="template_container"
style="background-color: #ffffff; border: 1px solid #dedede; box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1); border-radius: 3px;">
这种风格注入是如何以及在哪里完成的?
如何避免这种风格注入?
我想在模板中编写自己的内联样式。什么是正确的方法?
我注意到一些注入,比如上面的例子,依赖于元素的 ID,所以我可以删除它们的 ID,但其他情况来自一个变量,比如第 48 行的 templates/emails/email-header.php :
<h1><?php echo $email_heading; ?></h1>
它呈现为:
<h1 style="font-family: "Helvetica Neue", Helvetica, Roboto, Arial, sans-serif; font-size: 30px; font-weight: 300; line-height: 150%; margin: 0; text-align: left; text-shadow: 0 1px 0 #ffffff; color: #202020; background-color: inherit;">HTML email template</h1>
任何帮助表示赞赏。谢谢你。
解决方案
1)样式注入#template_container
可以在templates/emails/email-styles.php第 58 - 63 行 @version 4.0.0 中找到。
#template_container {
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1) !important;
background-color: <?php echo esc_attr( $body ); ?>;
border: 1px solid <?php echo esc_attr( $bg_darker_10 ); ?>;
border-radius: 3px !important;
}
2) $email_heading
调用模板文件时通过,见includes/class-wc-emails.php line 263 - 270 @version 2.3.0
/**
* Get the email header.
*
* @param mixed $email_heading Heading for the email.
*/
public function email_header( $email_heading ) {
wc_get_template( 'emails/email-header.php', array( 'email_heading' => $email_heading ) );
}
然而,样式注入也通过第 176 - 185 行 @version 4.0.0 上的 templates/emails/email-styles.php进行
h1 {
color: <?php echo esc_attr( $base ); ?>;
font-family: "Helvetica Neue", Helvetica, Roboto, Arial, sans-serif;
font-size: 30px;
font-weight: 300;
line-height: 150%;
margin: 0;
text-align: <?php echo is_rtl() ? 'right' : 'left'; ?>;
text-shadow: 0 1px 0 <?php echo esc_attr( $base_lighter_20 ); ?>;
}
所以回答你的问题:
如果您想应用自己的样式,或者想修改现有的样式。您将不得不覆盖模板文件。可以通过将模板文件复制到yourtheme/woocommerce/emails/email-styles.php
.
推荐阅读
- python - 组合数据帧切片时 concat 和 np_r 之间的不同结果
- json - 在 NodeJS 中检索 POST 的响应
- javascript - 在Javascript中处理字符串中所有字母的出现
- javascript - 如何使用plotly / Python / Dash从原点到用户的鼠标坐标绘制一条线
- php - 为什么 $nextRegistration->participants->contains 显示为 false?
- amazon-web-services - AWS Lambda 函数缓存。24小时缓存数据的最佳方式
- jquery - 单击div时如何将特定位置的文本输入文本框
- sql - 这两种索引创建模式有什么区别吗?
- python - 使用 concurrent.futures 进行多处理时出错
- reactjs - React.js - Flow.js 工作,但不检查属性的类型