Skip to content

Block Supports: Prevent Additional CSS duplication inside Query Loop#11859

Open
Mustafabharmal wants to merge 12 commits into
WordPress:trunkfrom
Mustafabharmal:fix/65268-prevent-additional-css-duplication-query-loop
Open

Block Supports: Prevent Additional CSS duplication inside Query Loop#11859
Mustafabharmal wants to merge 12 commits into
WordPress:trunkfrom
Mustafabharmal:fix/65268-prevent-additional-css-duplication-query-loop

Conversation

@Mustafabharmal

Copy link
Copy Markdown

Trac ticket: https://core.trac.wordpress.org/ticket/65268

What

When a block inside a Query Loop has Additional CSS, wp_add_inline_style was called once per post in the loop, duplicating the same CSS N times.

Why

wp_render_custom_css_support_styles hooks render_block_data, which fires for every block render. wp_unique_id_from_values produces the same hash for the same block template data on every loop iteration, so the same CSS gets injected repeatedly.

How

Added a static $enqueued_class_names array. The first time a class name is seen the CSS is enqueued; subsequent renders skip it.

Gutenberg PR: WordPress/gutenberg#78282

@github-actions

github-actions Bot commented May 19, 2026

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props mustafabharmal, westonruter, wildworks, mukesh27, masteradhoc.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@mukeshpanchal27 mukeshpanchal27 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you add unit tests that verify the changes?

Comment thread src/wp-includes/block-supports/custom-css.php Outdated
Comment thread src/wp-includes/block-supports/custom-css.php Outdated
Comment thread src/wp-includes/block-supports/custom-css.php Outdated
@westonruter

Copy link
Copy Markdown
Member

We should add a test for this as well.

Comment on lines +60 to +62
* Track which class names have already had their CSS enqueued to prevent
* duplicate styles when the same block is rendered multiple times inside
* a Query Loop (render_block_data fires once per loop iteration).

@t-hamano t-hamano Jun 7, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
* Track which class names have already had their CSS enqueued to prevent
* duplicate styles when the same block is rendered multiple times inside
* a Query Loop (render_block_data fires once per loop iteration).
* Skip CSS that has already been added. Blocks with identical attributes
* share the same class name and processed CSS via wp_unique_id_from_values(),
* so the same style would otherwise be enqueued more than once (e.g. inside
* a Query Loop or when blocks share identical custom CSS).

Since this issue is not limited to the Query Loop block, I recommend updating this comments to be more accurate.

For example, create content like the following:

<!-- wp:paragraph {"style":{"css":"background: red;"}} -->
<p class="has-custom-css">Hello World</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph {"style":{"css":"background: red;"}} -->
<p class="has-custom-css">Hello World</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph {"style":{"css":"background: red;"}} -->
<p class="has-custom-css">Hello World</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph {"style":{"css":"background: red;"}} -->
<p class="has-custom-css">Hello World</p>
<!-- /wp:paragraph -->

This PR will eliminate the duplicate CSS.

Before:

<style id="wp-block-custom-css-inline-css">
:root :where(.wp-custom-css-38db0572){background: red;}
:root :where(.wp-custom-css-38db0572){background: red;}
:root :where(.wp-custom-css-38db0572){background: red;}
:root :where(.wp-custom-css-38db0572){background: red;}
</style>

After:

<style id="wp-block-custom-css-inline-css">
:root :where(.wp-custom-css-38db0572){background: red;}
</style>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Good call, although I suggest {@see wp_unique_id_from_values()} to link the reference.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Applied in 8e767a4

@masteradhoc

Copy link
Copy Markdown

@Mustafabharmal Could you add some unit tests for this PR please? After the commit we can check as well if the failing check is working again. We'd like to get this a step further to potentially include it in WP 7.0.1

Thanks for your help here!

@westonruter westonruter requested a review from t-hamano July 4, 2026 01:15

@t-hamano t-hamano left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM! I would like to commit this later.

wp_add_inline_style( 'wp-block-custom-css', $processed_css );
$handle = 'wp-block-custom-css';
if ( ! wp_style_is( $handle, 'registered' ) ) {
wp_register_style( $handle, false, array( 'global-styles' ) );

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I have a slight concern about this code. What happens if a consumer intentionally deregister global-styles? In that case, global-styles would be forcibly registereded only when a block with custom CSS exists on the page, which might go against the consumer's intent. Shouldn't custom CSS operate independently of global styles?

However, since this logic was not introduced in this PR, it is not a blocker. We may be able to consider a fix in a follow-up.

@westonruter

Copy link
Copy Markdown
Member

LGTM! I would like to commit this later.

Go for it.

@t-hamano t-hamano dismissed mukeshpanchal27’s stale review July 6, 2026 03:01

Unblocking request changes as unit tests have been added.

@t-hamano

t-hamano commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

@pento, it appears that unnecessary changes were added due to the force push.

@mukeshpanchal27

Copy link
Copy Markdown
Member

@pento, it appears that unnecessary changes were added due to the force push.

+1

Comment thread composer.json
Comment on lines +20 to +22
"ext-ftp": "*",
"ext-mysqli": "*",
"ext-ssh2": "*"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🤔

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I added this in trunk via a13c8a2

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Something seems to have gone wrong in the repository. It looks like all open PRs were force-pushed by panto.

There may be an issue with the automation or repository configuration. Is anyone checking that?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Comment on lines +71 to 76
/**
* Skip CSS that has already been added. Blocks with identical attributes
* share the same class name and processed CSS via {@see wp_unique_id_from_values()},
* so the same style would otherwise be enqueued more than once (e.g. inside
* a Query Loop or when blocks share identical custom CSS).
*/

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
/**
* Skip CSS that has already been added. Blocks with identical attributes
* share the same class name and processed CSS via {@see wp_unique_id_from_values()},
* so the same style would otherwise be enqueued more than once (e.g. inside
* a Query Loop or when blocks share identical custom CSS).
*/
/*
* Skip CSS that has already been added. Blocks with identical attributes
* share the same class name and processed CSS via {@see wp_unique_id_from_values()},
* so the same style would otherwise be enqueued more than once (e.g. inside
* a Query Loop or when blocks share identical custom CSS).
*/

Nit - Use Multi-line comments https://developer.wordpress.org/coding-standards/inline-documentation-standards/php/#5-2-multi-line-comments

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The docblock style was intentional so that the inline @see tag is parsed.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Oh, I see. Let's put it back

@t-hamano t-hamano force-pushed the fix/65268-prevent-additional-css-duplication-query-loop branch from 8aabf1e to aa8abc4 Compare July 6, 2026 08:04
@t-hamano

t-hamano commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

I'm not entirely sure of the cause, but when I checked out the latest trunk locally and rebased this branch on top of it, the unintended changes seem to have disappeared.

Co-authored-by: Mukesh Panchal <mukeshpanchal27@users.noreply.github.com>
Comment thread src/wp-includes/block-supports/custom-css.php Outdated
* Register and add inline style for block custom CSS.
* The style depends on global-styles to ensure custom CSS loads after
* and can override global styles.
/**

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
/**
/*

Multi-line comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants