Make WordPress Core


Ignore:
Timestamp:
11/01/2025 04:44:21 AM (3 months ago)
Author:
westonruter
Message:

General: Rename wp_send_late_headers action to wp_finalized_template_enhancement_output_buffer.

Also update docs for wp_finalized_template_enhancement_output_buffer action and wp_template_enhancement_output_buffer filter to warn against attempting to open an output buffer in callbacks or else a PHP fatal error will occur.

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

Follow-up to [61088], [60936].

Props westonruter, dmsnell.
See #43258.
Fixes #64126.

File:
1 edited

Legend:

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

    r61088 r61111  
    845845     *
    846846     * By default, an output buffer is only started if a {@see 'wp_template_enhancement_output_buffer'} filter has been
    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.
     847     * added or if a plugin has added a {@see 'wp_finalized_template_enhancement_output_buffer'} action. For this
     848     * default to apply, either of the hooks must be added by the time the template is included at the
     849     * {@see 'wp_before_include_template'} action. This allows template responses to be streamed unless the there is
     850     * code which depends on an output buffer being opened. This filter allows a site to opt in to adding such template
     851     * enhancement filters later during the rendering of the template.
    852852     *
    853853     * @since 6.9.0
     
    855855     * @param bool $use_output_buffer Whether an output buffer is started.
    856856     */
    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' ) );
     857    return (bool) apply_filters( 'wp_should_output_buffer_template_for_enhancement', has_filter( 'wp_template_enhancement_output_buffer' ) || has_action( 'wp_finalized_template_enhancement_output_buffer' ) );
    858858}
    859859
     
    960960    if ( ! $is_html_content_type ) {
    961961        /** This action is documented in wp-includes/template.php */
    962         do_action( 'wp_send_late_headers', $output );
     962        do_action( 'wp_finalized_template_enhancement_output_buffer', $output );
    963963        return $output;
    964964    }
     
    976976     * fully supports HTML5.
    977977     *
     978     * Important: Because this filter is applied inside an output buffer callback (i.e. display handler), any callbacks
     979     * added to the filter must not attempt to start their own output buffers. Otherwise, PHP will raise a fatal error:
     980     * "Cannot use output buffering in output buffering display handlers."
     981     *
    978982     * @since 6.9.0
    979983     *
     
    984988
    985989    /**
    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
     990     * Fires after the template enhancement output buffer has been finalized.
     991     *
     992     * This happens immediately before the template enhancement output buffer is flushed. No output may be printed at
     993     * this action. However, HTTP headers may be sent, which makes this action complimentary to the
     994     * {@see 'send_headers'} action, in which headers may be sent before the template has started rendering. In
     995     * contrast, this `wp_finalized_template_enhancement_output_buffer` action is the possible point at which HTTP
     996     * headers can be sent. This action does not fire if the "template enhancement output buffer" was not started. This
     997     * output buffer is automatically started if this action is added before
     998     * {@see wp_start_template_enhancement_output_buffer()} runs at the {@see 'wp_before_include_template'} action with
     999     * priority 1000. Before this point, the output buffer will also be started automatically if there was a
    9941000     * {@see 'wp_template_enhancement_output_buffer'} filter added, or if the
    9951001     * {@see 'wp_should_output_buffer_template_for_enhancement'} filter is made to return `true`.
    9961002     *
     1003     * Important: Because this action fires inside an output buffer callback (i.e. display handler), any callbacks added
     1004     * to the action must not attempt to start their own output buffers. Otherwise, PHP will raise a fatal error:
     1005     * "Cannot use output buffering in output buffering display handlers."
     1006     *
    9971007     * @since 6.9.0
    9981008     *
    999      * @param string $output Output buffer.
     1009     * @param string $output Finalized output buffer.
    10001010     */
    1001     do_action( 'wp_send_late_headers', $filtered_output );
     1011    do_action( 'wp_finalized_template_enhancement_output_buffer', $filtered_output );
    10021012
    10031013    return $filtered_output;
Note: See TracChangeset for help on using the changeset viewer.