Skip to content
Closed
Changes from 1 commit
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
39a3907
Add output buffering for the rendered template
westonruter Feb 26, 2025
f576c3c
Rework filter to exclusively apply to an HTML output buffer
westonruter Mar 13, 2025
33b524b
Add wp_final_output_buffer action
westonruter Mar 13, 2025
5e49712
Merge branch 'trunk' of https://github.com/WordPress/wordpress-develo…
westonruter Mar 13, 2025
8a6f5a7
Merge branch 'trunk' of https://github.com/WordPress/wordpress-develo…
westonruter Sep 22, 2025
e9a46aa
Add wp_before_include_template action and move OB handling to new fun…
westonruter Sep 22, 2025
2c074fa
Add wp_template_output_buffer filter
westonruter Sep 22, 2025
7f79ebd
Ensure at least one tag is present for HTML content type
westonruter Sep 23, 2025
6637ef5
Add test for ending and cleaning an output buffer to avoid processing
westonruter Sep 23, 2025
b98b81e
Add test case for JSON
westonruter Sep 23, 2025
9f07ad0
Account for multiple content-type headers
westonruter Sep 23, 2025
4e7029b
Fix passing back the filtered output buffer
westonruter Sep 23, 2025
fd79c2a
Remove extra line break
westonruter Sep 23, 2025
322c930
Merge branch 'trunk' into trac-43258
westonruter Sep 26, 2025
32cc514
Add test for calling ob_clean() instead of ob_end_clean()
westonruter Sep 26, 2025
59cf047
Include original buffer in param to filters
westonruter Oct 1, 2025
b755a24
Fix alignment
westonruter Oct 1, 2025
2dabd76
Clarify that the wp_before_include_template action occurs immediately…
westonruter Oct 10, 2025
e65811c
Remove redundant/unreliable HTML content type check for the presence …
westonruter Oct 10, 2025
69df1d0
Eliminate non-HTML output buffering and remove action
westonruter Oct 10, 2025
853082e
Reframe output buffer as being for optimization only
westonruter Oct 10, 2025
d38dc86
Merge branch 'trunk' of https://github.com/WordPress/wordpress-develo…
westonruter Oct 10, 2025
7f309f5
Only start output buffer if filters are present by default
westonruter Oct 10, 2025
6ee2a45
Account for only the first listed content-type header being sent
westonruter Oct 11, 2025
1b4a2c4
Add missing filter param to phpdoc
westonruter Oct 11, 2025
7f43ee5
Fix typo
westonruter Oct 11, 2025
b242dc0
Be explicit about the DOM API to use
westonruter Oct 11, 2025
ad4c8d4
Clarify purpose of filter and why chunking is disabled
westonruter Oct 11, 2025
bbfdaa6
Merge branch 'trunk' into trac-43258
westonruter Oct 11, 2025
80d24ae
Refer to enhancement rather than optimization
westonruter Oct 11, 2025
16d7928
Add wp_should_output_buffer_template_for_enhancement() helper function
westonruter Oct 11, 2025
dfd6e5e
Add assertions for wp_template_enhancement_output_buffer_started action
westonruter Oct 11, 2025
5409e44
Remove unnecessary return phpdoc
westonruter Oct 12, 2025
44d52df
Remove unnecessary default params
westonruter Oct 12, 2025
6bcbf0e
Add missing assertion messages
westonruter Oct 12, 2025
d4a3da3
Merge branch 'trunk' of https://github.com/WordPress/wordpress-develo…
westonruter Oct 12, 2025
6333403
Improve docs for wp_finalize_template_enhancement_output_buffer()
westonruter Oct 15, 2025
611a482
Merge branch 'trunk' into trac-43258
westonruter Oct 15, 2025
f49563c
Remove whitespace at end of line
westonruter Oct 15, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Rework filter to exclusively apply to an HTML output buffer
Co-authored-by: Dennis Snell <dmsnell@git.wordpress.org>
  • Loading branch information
westonruter and dmsnell committed Mar 13, 2025
commit f576c3c3d67f7cbd680bbf66ba634f8471df0864
42 changes: 30 additions & 12 deletions src/wp-includes/template-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,36 @@ static function ( string $output, ?int $phase ): string {
return $output;
}

/**
* Filters the template output buffer prior to sending to the client.
*
* The output buffer is started before the `template_redirect` action is triggered, allowing templates rendered
* at that action to also have their output filtered.
*
* @since n.e.x.t
*
* @param string $output Output buffer.
* @return string Filtered output buffer.
*/
return (string) apply_filters( 'wp_template_output_buffer', $output );
// Detect if the response is an HTML content type.
$is_html_content_type = false;
$headers_list = array_merge(
array( 'Content-Type: ' . ini_get( 'default_mimetype' ) ),
headers_list()
);
foreach ( $headers_list as $header ) {
$header_parts = preg_split( '/\s*[:;]\s*/', strtolower( $header ) );
if ( is_array( $header_parts ) && count( $header_parts ) >= 2 && 'content-type' === $header_parts[0] ) {
$is_html_content_type = in_array( $header_parts[1], array( 'text/html', 'application/xhtml+xml' ), true );
}
}
// TODO: Also check if str_starts_with( ltrim( $buffer ), '<' )? Or check if str_contains( '<html' ) in case a PHP warning output an error before the HTML tag.

if ( $is_html_content_type ) {
/**
* Filters the HTML output buffer prior to sending to the client.
*
* The output buffer is started before the `template_redirect` action is triggered, allowing templates rendered
* at that action to also have their output filtered.
*
* @since n.e.x.t
*
* @param string $output Output buffer HTML.
* @return string Filtered output buffer HTML.
*/
$output = (string) apply_filters( 'wp_output_buffer_html', $output );
}

return $output;
},
0, // Unlimited buffer size so that entire output is passed to the filter.
/*
Expand Down