Make WordPress Core


Ignore:
Timestamp:
10/30/2025 12:46:29 AM (6 months ago)
Author:
westonruter
Message:

General: Add wp_send_late_headers action which fires right before the template enhancement output buffer is flushed.

This adds a (missing) wp_send_late_headers action which fires right after the wp_template_enhancement_output_buffer filters have applied and right before the output buffer is flushed. The filtered output buffer is passed as an argument to the action so that plugins may do things like send an ETag header which is calculated from the content. This action eliminates the need for plugins to hack the wp_template_enhancement_output_buffer filter with a high priority to send a late response header. This action compliments the send_headers action which is commonly used to send HTTP headers before the template is rendered. Furthermore:

  • The template enhancement output buffer is now enabled by default if there is a callback added to either the wp_template_enhancement_output_buffer filter or the wp_send_late_headers action.
  • The wp_start_template_enhancement_output_buffer() callback for the wp_before_include_template action is increased from the default of 10 to 1000. This goes with the previous point, so that plugins can add those filters and actions during the wp_before_include_template action without having to worry about adding them too late, that is, after wp_start_template_enhancement_output_buffer() has run.
  • The wp_send_late_headers action fires regardless of whether the buffered response is HTML.

Developed in https://github.com/WordPress/wordpress-develop/pull/10381

Follow-up to [60936].

Props westonruter, peterwilsoncc, johnbillion.
See #43258.
Fixes #64126.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/template.php

    r60994 r61088  
    845845     *
    846846     * By default, an output buffer is only started if a {@see 'wp_template_enhancement_output_buffer'} filter has been
    847      * added. For this default to apply, a filter must be added by the time the template is included at the
    848      * {@see 'wp_before_include_template'} action. This allows template responses to be streamed as much as possible
    849      * when no template enhancements are registered to apply. This filter allows a site to opt in to adding such
    850      * template enhancement filters during the rendering of the template.
     847     * added or if a plugin has added a {@see 'wp_send_late_headers'} action. For this default to apply, either of the
     848     * hooks must be added by the time the template is included at the {@see 'wp_before_include_template'} action. This
     849     * allows template responses to be streamed unless the there is code which depends on an output buffer being opened.
     850     * This filter allows a site to opt in to adding such template enhancement filters later during the rendering of the
     851     * template.
    851852     *
    852853     * @since 6.9.0
     
    854855     * @param bool $use_output_buffer Whether an output buffer is started.
    855856     */
    856     return (bool) apply_filters( 'wp_should_output_buffer_template_for_enhancement', has_filter( 'wp_template_enhancement_output_buffer' ) );
     857    return (bool) apply_filters( 'wp_should_output_buffer_template_for_enhancement', has_filter( 'wp_template_enhancement_output_buffer' ) || has_action( 'wp_send_late_headers' ) );
    857858}
    858859
     
    958959    // If the content type is not HTML, short-circuit since it is not relevant for enhancement.
    959960    if ( ! $is_html_content_type ) {
     961        /** This action is documented in wp-includes/template.php */
     962        do_action( 'wp_send_late_headers', $output );
    960963        return $output;
    961964    }
     
    978981     * @param string $output          Original HTML template output buffer.
    979982     */
    980     return (string) apply_filters( 'wp_template_enhancement_output_buffer', $filtered_output, $output );
    981 }
     983    $filtered_output = (string) apply_filters( 'wp_template_enhancement_output_buffer', $filtered_output, $output );
     984
     985    /**
     986     * Fires at the last moment HTTP headers may be sent.
     987     *
     988     * This happens immediately before the template enhancement output buffer is flushed. This is in contrast with
     989     * the {@see 'send_headers'} action which fires after the initial headers have been sent before the template
     990     * has begun rendering, and thus does not depend on output buffering. This action does not fire if the "template
     991     * enhancement output buffer" was not started. This output buffer is automatically started if this action is added
     992     * before {@see wp_start_template_enhancement_output_buffer()} runs at the {@see 'wp_before_include_template'}
     993     * action with priority 1000. Before this point, the output buffer will also be started automatically if there was a
     994     * {@see 'wp_template_enhancement_output_buffer'} filter added, or if the
     995     * {@see 'wp_should_output_buffer_template_for_enhancement'} filter is made to return `true`.
     996     *
     997     * @since 6.9.0
     998     *
     999     * @param string $output Output buffer.
     1000     */
     1001    do_action( 'wp_send_late_headers', $filtered_output );
     1002
     1003    return $filtered_output;
     1004}
Note: See TracChangeset for help on using the changeset viewer.