钩子文档

wp_finalized_template_enhancement_output_buffer

💡 云策文档标注

概述

wp_finalized_template_enhancement_output_buffer 是一个 WordPress 动作钩子,在模板增强输出缓冲区最终化后触发。它主要用于在缓冲区刷新前执行操作,但禁止输出内容,可用于发送 HTTP 头。

关键要点

  • 触发时机:在模板增强输出缓冲区最终化后、刷新前触发。
  • 输出限制:回调中禁止打印输出,否则在 PHP 8.5 前会静默忽略,之后会发出弃用通知。
  • HTTP 头:允许发送 HTTP 头,与 'send_headers' 动作互补,后者在模板渲染前发送头。
  • 条件触发:仅当模板增强输出缓冲区已启动时触发,否则不触发。
  • 自动启动:如果此动作在 'wp_before_include_template' 动作(优先级 1000)的 wp_start_template_enhancement_output_buffer() 运行前添加,或存在相关过滤器,缓冲区会自动启动。
  • 注意事项:回调中禁止启动新输出缓冲区,否则 PHP 会引发致命错误。

注意事项

  • 回调函数不得尝试启动自己的输出缓冲区,以避免 PHP 致命错误。

📄 原文内容

Fires after the template enhancement output buffer has been finalized.

Description

This happens immediately before the template enhancement output buffer is flushed. No output may be printed at this action; prior to PHP 8.5, the output will be silently omitted, whereas afterward a deprecation notice will be emitted. Nevertheless, HTTP headers may be sent, which makes this action complimentary to the ‘send_headers’ action, in which headers may be sent before the template has started rendering. In contrast, this wp_finalized_template_enhancement_output_buffer action is the possible point at which HTTP headers can be sent. This action does not fire if the “template enhancement output buffer” was not started.
This output buffer is automatically started if this action is added before wp_start_template_enhancement_output_buffer() runs at the ‘wp_before_include_template’ action with priority 1000. Before this point, the output buffer will also be started automatically if there was a ‘wp_template_enhancement_output_buffer’ filter added, or if the ‘wp_should_output_buffer_template_for_enhancement’ filter is made to return true.

Important: Because this action fires inside an output buffer callback (i.e. display handler), any callbacks added to the action must not attempt to start their own output buffers. Otherwise, PHP will raise a fatal error: “Cannot use output buffering in output buffering display handlers.”

Parameters

$outputstring
Finalized output buffer.

Source

do_action( 'wp_finalized_template_enhancement_output_buffer', $filtered_output );

Changelog

Version Description
6.9.0 Introduced.