Optimization Detective: Reuse the WordPress 6.9 template enhancement output buffer
Summary
Reuses the WordPress 6.9 template enhancement output buffer (Core-43258, see r60936) instead of Optimization Detective opening its own output buffer.
- Removes the
od_buffer_output()function and itstemplate_includehook (the "hack" referenced in its own docblock). od_maybe_add_template_output_buffer_filter()now registers the optimization callback on the corewp_template_enhancement_output_bufferfilter. Registering the filter on thewpaction (before core opens the buffer atwp_before_include_templatepriority 1000) opts in to the core buffer; when the response is not eligible for optimization, no filter is added and the response streams (an improvement over the old code, which always opened a passthrough buffer).
Fixes #2224.
Backward compatibility
The previously public, documented od_template_output_buffer filter is replaced by core's wp_template_enhancement_output_buffer. To avoid silently breaking any third-party callbacks, a backward-compatibility shim still applies od_template_output_buffer via apply_filters_deprecated() (emits a deprecation notice only when the filter actually has listeners). The entry was removed from docs/hooks.md and docs/introduction.md was updated.
Dependency
⚠️ This requires the minimum WordPress version to be 6.9 (#2354). The new hooks/functions only exist in 6.9+, and this PR intentionally omits function_exists() guards. Please land after/with the 6.9 minimum-version bump. (Companion to #2225, which performs the same migration for Performance Lab's Server-Timing.)
Known limitation
Core's buffer starts at wp_before_include_template, which only fires when the resolved template is a readable file. On routes where no readable template is included, optimization is not applied. This is an accepted trade-off of reusing the core buffer.
Test plan
- Removed the two tests that exercised
od_buffer_output()'s buffer mechanics (now owned by core). - Updated the eligibility test to assert registration on
wp_template_enhancement_output_buffer. - Added a test verifying the deprecated
od_template_output_bufferfilter is still applied (with@expectedDeprecated). optimization-detectivetestsuite: 355 tests, 12141 assertions — green on WP 7.0 (which includes the 6.9 API).image-prioritizer+embed-optimizer(OD consumers): green.phpcs+phpstanpass.
AI assistance
AI assistance: Yes Tool(s): Claude Code (Anthropic) Model(s): Claude Opus 4.8 Used for: Implementation, test updates, and code review (cross-checked with Grok); I have reviewed and take responsibility for the output.
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.
If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
Co-authored-by: MarcinDudekDev <[email protected]>
Co-authored-by: westonruter <[email protected]>
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.
Codecov Report
:white_check_mark: All modified and coverable lines are covered by tests.
:white_check_mark: Project coverage is 69.29%. Comparing base (4cbab3d) to head (99afc96).
Additional details and impacted files
@@ Coverage Diff @@
## trunk #2516 +/- ##
==========================================
- Coverage 69.29% 69.29% -0.01%
==========================================
Files 90 90
Lines 7723 7721 -2
==========================================
- Hits 5352 5350 -2
Misses 2371 2371
| Flag | Coverage Δ | |
|---|---|---|
| multisite | 69.29% <100.00%> (-0.01%) |
:arrow_down: |
| single | 35.64% <0.00%> (-0.13%) |
:arrow_down: |
Flags with carried forward coverage won't be shown. Click here to find out more.
:umbrella: View full report in Codecov by Harness.
:loudspeaker: Have feedback on the report? Share it here.
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
- :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.