Skip to content

Add theme.json block stylesheet support + bug fix#2

Open
joeleenk wants to merge 2 commits into
mainfrom
theme-json-support
Open

Add theme.json block stylesheet support + bug fix#2
joeleenk wants to merge 2 commits into
mainfrom
theme-json-support

Conversation

@joeleenk
Copy link
Copy Markdown

@joeleenk joeleenk commented May 19, 2026

  • Adds get_extended_block_stylesheets function to look through the theme.json for stylesheet references
  • Adds after_setup_theme action to register those stylesheets
  • Registers and enqueues the stylesheets only if those blocks are on the page
  • Bug fix for block style enqueue timing issue

Note: this is vibe-coded and human reviewed.

@joeleenk joeleenk requested a review from kadamwhite May 19, 2026 14:09
@joeleenk joeleenk changed the title Add theme.json block stylesheet support Add theme.json block stylesheet support + bug fix May 19, 2026
Copy link
Copy Markdown
Collaborator

@kadamwhite kadamwhite left a comment

Choose a reason for hiding this comment

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

Before approving, I want to understand the specific timing issue requiring changing the hook to render_block, and to understand the use case for registering blocks in theme.json rather than using the existing block.json properties?

Comment thread inc/namespace.php
*/
add_filter(
$hook_name,
'render_block',
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can you explain the bugfix? The original plan to hook into render_block_ was deliberate to substantially reduce the number of times we need to run these checks, on large documents — I'd love to know more about the timing issue that's alluded to here.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

The stylesheet loading was working for other blocks, but when I got to the cover block, the styles weren't being enqueued. With Claude's help, I discovered that they only took effect for the second block of the same block style.

Here's Claude's explanation:

The original code hooked into render_block_{block_name} at priority 10, but that made it impossible for wp_enqueue_block_style() to actually enqueue the stylesheet for the current block.

Here's why:

wp_enqueue_block_style() works by adding its own callback to the generic render_block filter at priority 10. When a block renders, that callback checks whether the style is queued and injects it.

The original code called wp_enqueue_block_style() from inside a render_block_{block_name} hook — but WordPress fires the generic render_block hook before the block-specific render_block_{block_name} hook. This means by the time our callback ran and called wp_enqueue_block_style(), the render_block hook had already fired for that block, so wp_enqueue_block_style()'s own internal callback would never run for it. The style effectively got registered too late — only on the next block of that type, if one existed.

The fix switched the hook from render_block_{block_name} (priority 10) to render_block at priority 1. Since priority 1 fires before wp_enqueue_block_style()'s own priority-10 callback on the same render_block hook, our code now calls wp_enqueue_block_style() while there's still time for it to do its work on the current block. The block name check ($block_name === $block['blockName']) was also added to the condition to compensate for no longer using the block-specific hook.

@joeleenk
Copy link
Copy Markdown
Author

For the change to look for stylesheets in theme.json, that was so that we can use media and container queries for the default block style/all block styles for the block. This was Claude's idea of how to do it - I'm not attached to it, if we can find another way to use query-based styles that only load when the block is on the page.

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.

2 participants