wp_start_template_enhancement_output_buffer(): bool

In this article

Starts the template enhancement output buffer.

Description

This function is called immediately before the template is included.

Return

bool Whether the output buffer successfully started.

Source

function wp_start_template_enhancement_output_buffer(): bool {
	if ( ! wp_should_output_buffer_template_for_enhancement() ) {
		return false;
	}

	$started = ob_start(
		'wp_finalize_template_enhancement_output_buffer',
		0, // Unlimited buffer size so that entire output is passed to the filter.
		/*
		 * Instead of the default PHP_OUTPUT_HANDLER_STDFLAGS (cleanable, flushable, and removable) being used for
		 * flags, the PHP_OUTPUT_HANDLER_FLUSHABLE flag must be omitted. If the buffer were flushable, then each time
		 * that ob_flush() is called, a fragment of the output would be sent into the output buffer callback. This
		 * output buffer is intended to capture the entire response for processing, as indicated by the chunk size of 0.
		 * So the buffer does not allow flushing to ensure the entire buffer can be processed, such as for optimizing an
		 * entire HTML document, where markup in the HEAD may need to be adjusted based on markup that appears late in
		 * the BODY.
		 *
		 * If this ends up being problematic, then PHP_OUTPUT_HANDLER_FLUSHABLE could be added to the $flags and the
		 * output buffer callback could check if the phase is PHP_OUTPUT_HANDLER_FLUSH and abort any subsequent
		 * processing while also emitting a _doing_it_wrong().
		 *
		 * The output buffer needs to be removable because WordPress calls wp_ob_end_flush_all() and then calls
		 * wp_cache_close(). If the buffers are not all flushed before wp_cache_close() is closed, then some output buffer
		 * handlers (e.g. for caching plugins) may fail to be able to store the page output in the object cache.
		 * See <https://github.com/WordPress/performance/pull/1317#issuecomment-2271955356>.
		 */
		PHP_OUTPUT_HANDLER_STDFLAGS ^ PHP_OUTPUT_HANDLER_FLUSHABLE
	);

	if ( $started ) {
		/**
		 * Fires when the template enhancement output buffer has started.
		 *
		 * @since 6.9.0
		 */
		do_action( 'wp_template_enhancement_output_buffer_started' );
	}

	return $started;
}

Hooks

do_action( ‘wp_template_enhancement_output_buffer_started’ )

Fires when the template enhancement output buffer has started.

Changelog

VersionDescription
6.9.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.