wppaste
WordPress

wp_finalize_template_enhancement_output_buffer( string $output, int $phase ): string

Since
6.9.0
Source
wp-includes/template.php:924
Finalizes the template enhancement output buffer.

Description

Checks to see if the output buffer is complete and contains HTML. If so, runs the content through the wp_template_enhancement_output_buffer filter. If not, the original content is returned.

Compatibility

WordPress
since 6.9.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in 3 of the 5 tracked releases, added in 6.9.0, and compiles on PHP 7.4 through 8.6-dev.

Parameters

$outputstring
Output buffer.
$phaseint
Phase.

Return value

string
Finalized output buffer.

Performance profile

How much work a call to wp_finalize_template_enhancement_output_buffer() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
14–23

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 212.

Plugin surface
2 hooks

Third-party callbacks on 'wp_finalized_template_enhancement_output_buffer', 'wp_template_enhancement_output_buffer' run inside this call, and their cost is not bounded by anything here.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • hookthird-party callbacksdo_action()called directly

Further down the call graph this can also reach query, option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 3 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_finalize_template_enhancement_output_buffer() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
$level !== 256 && !$level14–15error_reporting()
$level === 25617__()
$level !== 256 && $level22–23error_reporting(), compact()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev21214–2319
8.521214–2319
8.421214–23199 fewer instructions than PHP 8.3
8.322114–2319
8.222114–23191 more instruction than PHP 8.1
8.122014–2319
7.422014–2319

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 3

3 hooks fire while wp_finalize_template_enhancement_output_buffer() runs, in this order:

  1. do_action( wp_finalized_template_enhancement_output_buffer )actionline 962 (+38 into the body)

    Fires after the template enhancement output buffer has been finalized.

  2. apply_filters( wp_template_enhancement_output_buffer )filterline 1019 (+95 into the body)

    Filters the template enhancement output buffer prior to sending to the client.

  3. do_action( wp_finalized_template_enhancement_output_buffer )actionline 1058 (+134 into the body)

    Fires after the template enhancement output buffer has been finalized.

Uses · 4

Source code

function wp_finalize_template_enhancement_output_buffer( string $output, int $phase ): string {	// When the output is being cleaned (e.g. pending template is replaced with error page), do not send it through the filter.	if ( ( $phase & PHP_OUTPUT_HANDLER_CLEAN ) !== 0 ) {		return $output;	} 	// Detect if the response is an HTML content type.	$is_html_content_type = null;	$html_content_types   = array( 'text/html', 'application/xhtml+xml' );	foreach ( headers_list() as $header ) {		$header_parts = explode( ':', strtolower( $header ), 2 );		if (			count( $header_parts ) === 2 &&			'content-type' === $header_parts[0]		) {			/*			 * This is looking for very specific content types, therefore it			 * doesn’t need to fully parse the header’s value. Instead, it needs			 * only assert that the content type is one of the static HTML types.			 *			 * Example:			 *			 *     Content-Type: text/html; charset=utf8			 *     Content-Type: text/html  ;charset=latin4			 *     Content-Type:application/xhtml+xml			 */			$media_type           = trim( strtok( $header_parts[1], ';' ), " \t" );			$is_html_content_type = in_array( $media_type, $html_content_types, true );			break; // PHP only sends the first Content-Type header in the list.		}	}	if ( null === $is_html_content_type ) {		$is_html_content_type = in_array( ini_get( 'default_mimetype' ), $html_content_types, true );	} 	// If the content type is not HTML, short-circuit since it is not relevant for enhancement.	if ( ! $is_html_content_type ) {		/** This action is documented in wp-includes/template.php */		do_action( 'wp_finalized_template_enhancement_output_buffer', $output );		return $output;	} 	$filtered_output = $output; 	$did_just_catch = false; 	$error_log = array();	set_error_handler(		static function ( int $level, string $message, ?string $file = null, ?int $line = null ) use ( &$error_log, &$did_just_catch ) {			// Switch a user error to an exception so that it can be caught and the buffer can be returned.			if ( E_USER_ERROR === $level ) {				throw new Exception( __( 'User error triggered:' ) . ' ' . $message );			} 			// Display a caught exception as an error since it prevents any of the output buffer filters from applying.			if ( $did_just_catch ) {				$level = E_USER_ERROR;			} 			// Capture a reported error to be displayed by appending to the processed output buffer if display_errors is enabled.			if ( error_reporting() & $level ) {				$error_log[] = compact( 'level', 'message', 'file', 'line' );			}			return false;		}	);	$original_display_errors = ini_get( 'display_errors' );	if ( $original_display_errors ) {		ini_set( 'display_errors', 0 );	} 	try {		/**		 * Filters the template enhancement output buffer prior to sending to the client.		 *		 * This filter only applies the HTML output of an included template. This filter is a progressive enhancement		 * intended for applications such as optimizing markup to improve frontend page load performance. Sites must not		 * depend on this filter applying since they may opt to stream the responses instead. Callbacks for this filter		 * are highly discouraged from using regular expressions to do any kind of replacement on the output. Use the		 * HTML API (either `WP_HTML_Tag_Processor` or `WP_HTML_Processor`), or else use {@see DOM\HtmlDocument} as of

Changelog

Introduced in 6.9.0. Unchanged from 6.9.7 through 7.1.0.

  1. 6.9.7
  2. 7.0.4
  3. 7.1.0

Signature, return type and hooks compared across 3 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/template.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.