Skip to content

Block Hooks: Skip parsing and re-serializing post content in the_content when no blocks are hooked - #14022

Open
sboisvert wants to merge 2 commits into
WordPress:trunkfrom
sboisvert:66236-skip-block-hooks-parse-when-none-hooked
Open

sboisvert wants to merge 2 commits into
WordPress:trunkfrom
sboisvert:66236-skip-block-hooks-parse-when-none-hooked

Conversation

@sboisvert

@sboisvert sboisvert commented Oct 5, 2026 •

Copy link
Copy Markdown

Pages with a lot of block content are slow because, since 6.8, every the_content call parses and re-serializes the whole post even when no block is hooked. Since [60173] (6.8.1), the temporary hooked_block_types filter also makes the has_filter( 'hooked_block_types' ) check in apply_block_hooks_to_content() always true on this path, so the per-block Block Hooks run on top of that.

Timing apply_block_hooks_to_content_from_post_object() on ~1 MB of block markup with nothing hooked (PHP 8.4): 34-35 ms per call on trunk, 0.4 ms with this change.

This change returns the content unchanged at the top of apply_block_hooks_to_content_from_post_object(), before the temporary filter is added, when all of these are true:

  • get_hooked_blocks() is empty and has_filter( 'hooked_block_types' ) is false;
  • has_block( 'core/template-part', $content ) is false;
  • $ignored_hooked_blocks_at_root is null (the REST API passes an empty array, so REST requests still get the full parse; see #65008).

Rendered output is unchanged, except for content that begins with a stray closing block delimiter. Since 6.8, the function wraps the post content in a temporary wrapper block before parsing, and that wrapper absorbs a leading stray closer, so the blocks after it render. With this change, such content renders as it did before 6.8.

Adds test_apply_block_hooks_to_content_from_post_object_returns_content_unchanged_if_no_hooked_blocks to applyBlockHooksToContentFromPostObject.php. Run with php vendor/bin/phpunit --group block-hooks.

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

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code, Codex CLI
Model(s): Claude Opus 5.5, GPT-6 Astra
Used for: root-cause analysis, git history checks, patch, test and benchmark drafting, accuracy review;
I reviewed the patch and the claims.

@github-actions

github-actions Bot commented Oct 5, 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 sboisvert.

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

}
}

$parser_calls = 0;

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.

You can use the MockAction class for this instead. It has a method that gets the call count.

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