代码之家  ›  专栏  ›  技术社区  ›  aitor

Woocommerce自定义电子邮件模板:如何避免风格注入

  •  0
  • aitor  · 技术社区  · 5 年前

    模板中有一些HTML元素,例如, templates/emails/email-header.php 第41行 table#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,但其他情况是以变量形式出现的,比如 templates/emails/email-header.php 第48行:

    <h1><?php echo $email_heading; ?></h1>
    

    它被呈现为:

    <h1 style="font-family: &quot;Helvetica Neue&quot;, 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>
    

    感谢您的帮助。非常感谢。

    0 回复  |  直到 5 年前
        1
  •  2
  •   7uc1f3r    5 年前

    1) 来自 #template_container 可以在 templates/emails/email-styles.php 第58-63行@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 第263-270行@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 ) );
    }
    

    然而,风格注入也通过 templates/emails/email-styles.php 第176-185行@4.0.0版

    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 .

    另见: Template structure & Overriding templates via a theme - How to Edit files