函数文档

_add_template_loader_filters()

💡 云策文档标注

概述

_add_template_loader_filters() 函数用于为 WordPress 主题模板加载添加必要的钩子,以处理 '_wp-find-template' 请求,主要针对支持块模板的主题。

关键要点

  • 函数检查是否存在 '_wp-find-template' 查询参数且当前主题支持 'block-templates' 功能。
  • 如果条件满足,它会添加一个 pre_get_posts 动作钩子,调用 _resolve_template_for_new_post 函数。
  • 此函数自 WordPress 5.9.0 版本引入,用于增强块模板的解析能力。

代码示例

function _add_template_loader_filters() {
    if ( isset( $_GET['_wp-find-template'] ) && current_theme_supports( 'block-templates' ) ) {
        add_action( 'pre_get_posts', '_resolve_template_for_new_post' );
    }
}

注意事项

  • 此函数仅在特定条件下触发,依赖于主题功能和查询参数,开发者需确保主题正确支持 'block-templates'。
  • 使用 add_action 添加钩子时,应确保 _resolve_template_for_new_post 函数已定义或可用。

📄 原文内容

Adds necessary hooks to resolve ‘_wp-find-template’ requests.

Source

function _add_template_loader_filters() {
	if ( isset( $_GET['_wp-find-template'] ) && current_theme_supports( 'block-templates' ) ) {
		add_action( 'pre_get_posts', '_resolve_template_for_new_post' );
	}
}

Changelog

Version Description
5.9.0 Introduced.