函数文档

wp_should_output_buffer_template_for_enhancement()

💡 云策文档标注

概述

wp_should_output_buffer_template_for_enhancement() 函数用于检查模板是否应启用输出缓冲以支持增强功能。默认情况下,输出缓冲仅在特定 Hook 被添加时启动,以优化模板响应流。

关键要点

  • 函数返回布尔值,指示是否应为模板增强启用输出缓冲。
  • 默认行为依赖于 'wp_template_enhancement_output_buffer' 过滤器或 'wp_finalized_template_enhancement_output_buffer' 动作在 'wp_before_include_template' 动作前是否被添加。
  • 通过 'wp_should_output_buffer_template_for_enhancement' 过滤器,站点可以动态控制输出缓冲的启用时机。
  • 函数内部使用 has_filter() 和 has_action() 检查相关 Hook 的注册状态。

代码示例

function wp_should_output_buffer_template_for_enhancement(): bool {
    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' ) );
}

注意事项

  • 此函数自 WordPress 6.9.0 版本引入。
  • 输出缓冲的启用有助于模板增强功能的实现,但可能影响性能,需根据实际需求配置。
  • 相关 Hook 包括 'wp_template_enhancement_output_buffer' 和 'wp_finalized_template_enhancement_output_buffer',开发者可通过它们扩展功能。

📄 原文内容

Checks whether the template should be output buffered for enhancement.

Description

By default, an output buffer is only started if a ‘wp_template_enhancement_output_buffer’ filter has been added by the time a template is included at the ‘wp_before_include_template’ action. This allows template responses to be streamed as much as possible when no template enhancements are registered to apply.

Return

bool Whether the template should be output-buffered for enhancement.

Source

function wp_should_output_buffer_template_for_enhancement(): bool {
	/**
	 * Filters whether the template should be output-buffered for enhancement.
	 *
	 * By default, an output buffer is only started if a 'wp_template_enhancement_output_buffer' filter has been
	 * added or if a plugin has added a 'wp_finalized_template_enhancement_output_buffer' action. For this
	 * default to apply, either of the hooks must be added by the time the template is included at the
	 * 'wp_before_include_template' action. This allows template responses to be streamed unless the there is
	 * code which depends on an output buffer being opened. This filter allows a site to opt in to adding such template
	 * enhancement filters later during the rendering of the template.
	 *
	 * @since 6.9.0
	 *
	 * @param bool $use_output_buffer Whether an output buffer is started.
	 */
	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' ) );
}

Hooks

apply_filters( ‘wp_should_output_buffer_template_for_enhancement’, bool $use_output_buffer )

Filters whether the template should be output-buffered for enhancement.

Changelog

Version Description
6.9.0 Introduced.