Skip to content

Add filter to disable inline css for custom html email templates to resolve Emogrifier styling issues. #58813

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from

Conversation

mryoubou
Copy link

@mryoubou mryoubou commented Jun 13, 2025

Submission Review Guidelines:

Changes proposed in this Pull Request:

This PR introduces a new filter woocommerce_email_style_inline, allowing developers to selectively disable WooCommerce's automatic inline CSS styling for specific email templates.

By default, WooCommerce uses the Emogrifier library in the style_inline( $content ) method to inline styles for HTML emails. However, this behavior can be problematic when using custom-built responsive email templates:

  • Emogrifier strips out <style> tags placed in the of custom templates, breaking responsive layouts.

  • Since our html email templates are already styled and optimized, running them through Emogrifier introduces unnecessary processing and causes layout issues.

  • Disabling Emogrifier for such templates helps preserve intended styles and improves performance.

This filter give us control to opt out of the inline style process for specific emails, making it safer and more efficient to use custom HTML templates in WooCommerce emails.

Screenshots or screen recordings:

With inline styles.

with-emogrifier.mp4

Without inline styles.

without-emogrifier.mp4

Summary by CodeRabbit

  • New Features
    • Added the ability for external plugins or custom code to dynamically enable or disable inline CSS styling in WooCommerce emails.

Copy link
Contributor

coderabbitai bot commented Jun 13, 2025

Walkthrough

The change introduces a new filter, woocommerce_email_style_inline, within the style_inline method of the WooCommerce email class. This filter allows external code to control whether inline CSS styles are applied to email content, in addition to the existing content type checks.

Changes

File Change Summary
plugins/woocommerce/includes/emails/class-wc-email.php Added woocommerce_email_style_inline filter to conditionally apply inline CSS in email content.

Sequence Diagram(s)

sequenceDiagram
    participant ExternalCode
    participant WC_Email
    participant EmailContent

    WC_Email->>ExternalCode: apply_filters('woocommerce_email_style_inline', true, $this)
    ExternalCode-->>WC_Email: Returns true/false
    WC_Email->>WC_Email: Check content type and filter result
    alt Inline styling allowed
        WC_Email->>EmailContent: Apply inline CSS styles
    else Inline styling not allowed
        WC_Email->>EmailContent: Skip inline CSS styling
    end
Loading
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-actions github-actions bot added plugin: woocommerce Issues related to the WooCommerce Core plugin. type: community contribution labels Jun 13, 2025
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e59ec62 and 12d7640.

📒 Files selected for processing (1)
  • plugins/woocommerce/includes/emails/class-wc-email.php (1 hunks)

Comment on lines 807 to 808
public function style_inline( $content ) {
if ( in_array( $this->get_content_type(), array( 'text/html', 'multipart/alternative' ), true ) ) {
if ( apply_filters( 'woocommerce_email_style_inline', true, $this ) && in_array( $this->get_content_type(), array( 'text/html', 'multipart/alternative' ), true ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Document woocommerce_email_style_inline filter in the style_inline() docblock and add test coverage.

The newly introduced woocommerce_email_style_inline filter isn’t described in the PHPDoc above style_inline(). Please update the docblock to include an @filter tag, for example:

/**
 * @filter bool woocommerce_email_style_inline
 *        Whether to apply inline CSS to the email content. Default true.
 * @param bool     $apply   Whether to inline styles.
 * @param WC_Email $email   The email instance.
 */

Additionally, add unit tests to confirm that:

  • Inline CSS is applied when the filter returns true.
  • Styling is left intact (no inlining) when the filter returns false.
🤖 Prompt for AI Agents
In plugins/woocommerce/includes/emails/class-wc-email.php around lines 807 to
808, the docblock for the style_inline() method lacks documentation for the
woocommerce_email_style_inline filter. Update the PHPDoc above style_inline() to
include an @filter tag describing the filter's purpose, parameters, and default
value as shown in the example. Additionally, add unit tests to verify that
inline CSS is applied when the filter returns true and that styling remains
unchanged when the filter returns false.

@mryoubou mryoubou changed the title Add Filter to Disable Inline CSS for Custom Email Templates to Resolve Emogrifier Styling Issues and Improve Performance Add filter to disable inline css for custom html email templates to resolve Emogrifier styling issues. Jun 13, 2025
@nerrad
Copy link
Contributor

nerrad commented Jul 30, 2025

Rather than the filter be for implementing a simple boolean flag, I think it'd be better to filter over the use of actual content being processed so that it can be used to swap out what any inline style handling is applied.

@mryoubou
Copy link
Author

mryoubou commented Jul 31, 2025

Rather than the filter be for implementing a simple boolean flag, I think it'd be better to filter over the use of actual content being processed so that it can be used to swap out what any inline style handling is applied.

Yes, something like this would be great:

$style_inline_callback  = apply_filters( 'woocommerce_mail_style_inline_callback', array( $this, 'style_inline' ), $this );

if ( is_callable( $style_inline_callback ) ) {
    $message = apply_filters( 'woocommerce_mail_content', call_user_func( $style_inline_callback, $message ), $message, $this); 
}else{
    $message = apply_filters( 'woocommerce_mail_content', $this->style_inline( $message ), $message, $this ); 
}

@triple0t
Copy link
Contributor

triple0t commented Aug 5, 2025

Hi @mryoubou, can you apply this change to the PR?

@mryoubou mryoubou closed this Aug 6, 2025
@github-actions github-actions bot removed the plugin: woocommerce Issues related to the WooCommerce Core plugin. label Aug 6, 2025
@mryoubou mryoubou reopened this Aug 6, 2025
@github-actions github-actions bot added the plugin: woocommerce Issues related to the WooCommerce Core plugin. label Aug 6, 2025
@mryoubou
Copy link
Author

mryoubou commented Aug 6, 2025

@triple0t All set! I just pushed the change

@triple0t
Copy link
Contributor

triple0t commented Aug 6, 2025

Hi @mryoubou, I can see some CI checks are failing.

  • Lint: pnpm --filter='@woocommerce/plugin-woocommerce' lint:changes:branch
  • Changelog. Please add a changelog with pnpm --filter='@woocommerce/plugin-woocommerce' changelog add

@mryoubou
Copy link
Author

Hi @triple0t, I’ve fixed the lint issues and added the changelog

@triple0t
Copy link
Contributor

Hi @mryoubou, Thank you for working with me on this PR.
I appreciate your time and effort.

The lint check is still failing, and we can't merge this PR unless it passes (the CI will start failing for everyone else).

Please run pnpm --filter='@woocommerce/plugin-woocommerce' lint:changes:branch to check the lint errors.

Screenshot 2025-08-11 at 8 59 00 AM

@mryoubou
Copy link
Author

I just found out that the style_inline() method is used twice in the EmailPreview class and unit tests. I think a better approach would be to add the callback filter inside the style_inline() function without changing anything in the send() function.

I will submit those changes to the PR soon.

$inlined = $this->email->style_inline( $content );

$content = $this->get_email()->style_inline( $content );

@mryoubou mryoubou closed this Aug 11, 2025
@github-actions github-actions bot removed the plugin: woocommerce Issues related to the WooCommerce Core plugin. label Aug 11, 2025
@mryoubou mryoubou reopened this Aug 11, 2025
@github-actions github-actions bot added the plugin: woocommerce Issues related to the WooCommerce Core plugin. label Aug 11, 2025
@mryoubou
Copy link
Author

Hi @triple0t, I just pushed the new changes. Could you help me with what to put in the changelog, as well as the @version and @since in the hooks/functions comments?

@triple0t
Copy link
Contributor

triple0t commented Aug 12, 2025

Hi @mryoubou

Could you help me with what to put in the changelog,

I believe the previous text should suffice.

as well as the @version and @since in the hooks/functions comments?

10.2.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plugin: woocommerce Issues related to the WooCommerce Core plugin. type: community contribution
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants