钩子文档

pre_get_block_file_template

💡 云策文档标注

概述

pre_get_block_file_template 是一个 WordPress 过滤器,用于在主题文件发现之前拦截和修改块模板对象。开发者可以通过此 Hook 返回自定义模板对象以绕过默认查询流程。

关键要点

  • 过滤器在主题文件发现前执行,允许开发者提前干预模板获取过程。
  • 返回非 null 值可以短路 WordPress 的默认查询,直接使用自定义模板对象。
  • 参数包括 $block_template(模板对象或 null)、$id(模板唯一标识符)和 $template_type(模板类型)。
  • 相关函数 get_block_file_template() 用于基于主题文件或插件注册检索统一模板对象。

代码示例

$block_template = apply_filters( 'pre_get_block_file_template', null, $id, $template_type );

📄 原文内容

Filters the block template object before the theme file discovery takes place.

Description

Return a non-null value to bypass the WordPress theme file discovery.

Parameters

$block_templateWP_Block_Template|null
Return block template object to short-circuit the default query, or null to allow WP to run its normal queries.
$idstring
Template unique identifier (example: 'theme_slug//template_slug').
$template_typestring
Template type. Either 'wp_template' or 'wp_template_part'.

Source

$block_template = apply_filters( 'pre_get_block_file_template', null, $id, $template_type );

Changelog

Version Description
5.9.0 Introduced.