Skip to content

Commit 69943b1

Browse files
committed
Trigger wp_send_late_headers even when the output buffer is not HTML
1 parent 158cb5f commit 69943b1

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

src/wp-includes/class-wp.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,9 @@ public function send_headers() {
588588
/**
589589
* Fires once the requested HTTP headers for caching, content type, etc. have been sent.
590590
*
591+
* The {@see 'wp_send_late_headers'} action may be used to send headers after rendering the template into an
592+
* output buffer.
593+
*
591594
* @since 2.1.0
592595
*
593596
* @param WP $wp Current WordPress environment instance (passed by reference).

src/wp-includes/template.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,21 @@ function wp_start_template_enhancement_output_buffer(): bool {
922922
* @return string Finalized output buffer.
923923
*/
924924
function wp_finalize_template_enhancement_output_buffer( string $output, int $phase ): string {
925+
$do_send_late_headers = static function ( string $output ): void {
926+
/**
927+
* Fires at the last moment HTTP headers may be sent.
928+
*
929+
* This happens immediately before the template enhancement output buffer is flushed. This is in contrast with
930+
* the {@see 'send_headers'} action which fires after the initial headers have been sent before the template
931+
* has begun rendering, and thus does not depend on output buffering.
932+
*
933+
* @since 6.9.0
934+
*
935+
* @param string $output Output buffer.
936+
*/
937+
do_action( 'wp_send_late_headers', $output );
938+
};
939+
925940
// When the output is being cleaned (e.g. pending template is replaced with error page), do not send it through the filter.
926941
if ( ( $phase & PHP_OUTPUT_HANDLER_CLEAN ) !== 0 ) {
927942
return $output;
@@ -958,6 +973,7 @@ function wp_finalize_template_enhancement_output_buffer( string $output, int $ph
958973

959974
// If the content type is not HTML, short-circuit since it is not relevant for enhancement.
960975
if ( ! $is_html_content_type ) {
976+
$do_send_late_headers( $output );
961977
return $output;
962978
}
963979

@@ -980,16 +996,7 @@ function wp_finalize_template_enhancement_output_buffer( string $output, int $ph
980996
*/
981997
$filtered_output = (string) apply_filters( 'wp_template_enhancement_output_buffer', $filtered_output, $output );
982998

983-
/**
984-
* Fires at the last moment HTTP headers may be sent.
985-
*
986-
* This happens immediately after the template enhancement output buffer has been processed.
987-
*
988-
* @since 6.9.0
989-
*
990-
* @param string $filtered_output Filtered output buffer.
991-
*/
992-
do_action( 'wp_send_late_headers', $filtered_output );
999+
$do_send_late_headers( $filtered_output );
9931000

9941001
return $filtered_output;
9951002
}

0 commit comments

Comments
 (0)