钩子文档

wp_template_enhancement_output_buffer

💡 云策文档标注

概述

wp_template_enhancement_output_buffer 是一个 WordPress 过滤器,用于在将模板增强输出缓冲区发送到客户端之前进行过滤。它主要应用于 HTML 输出,旨在优化前端页面加载性能,但站点不应依赖此过滤器,因为响应可能被流式传输。

关键要点

  • 此过滤器仅适用于包含模板的 HTML 输出,用于渐进增强,如优化标记以提高前端性能。
  • 站点不得依赖此过滤器,因为可能选择流式传输响应。
  • 回调函数应避免使用正则表达式进行输出替换,推荐使用 HTML API(如 WP_HTML_Tag_Processor 或 WP_HTML_Processor)或 PHP 8.4+ 的 DOMHtmlDocument。
  • 在此过滤器期间不要打印任何输出,否则在 PHP 8.5 前会静默忽略,之后会发出弃用通知。
  • 回调函数不得尝试启动自己的输出缓冲区,否则 PHP 会引发致命错误。

注意事项

  • 过滤器应用于输出缓冲区回调中,因此回调函数必须避免启动新输出缓冲区。
  • 使用 HTML API 或 DOMHtmlDocument 进行 HTML 处理,以确保兼容性和安全性。

📄 原文内容

Filters the template enhancement output buffer prior to sending to the client.

Description

This filter only applies the HTML output of an included template. This filter is a progressive enhancement intended for applications such as optimizing markup to improve frontend page load performance. Sites must not depend on this filter applying since they may opt to stream the responses instead. Callbacks for this filter are highly discouraged from using regular expressions to do any kind of replacement on the output. Use the HTML API (either WP_HTML_Tag_Processor or WP_HTML_Processor), or else use DOMHtmlDocument as of PHP 8.4 which fully supports HTML5.

Do not print any output during this filter. While filters normally don’t print anything, this is especially important since this applies during an output buffer callback. Prior to PHP 8.5, the output will be silently omitted, whereas afterward a deprecation notice will be emitted.

Important: Because this filter is applied inside an output buffer callback (i.e. display handler), any callbacks added to the filter 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

$filtered_outputstring
HTML template enhancement output buffer.
$outputstring
Original HTML template output buffer.

Source

$filtered_output = (string) apply_filters( 'wp_template_enhancement_output_buffer', $filtered_output, $output );

Changelog

Version Description
6.9.0 Introduced.