钩子文档

wp_before_load_template

💡 云策文档标注

概述

wp_before_load_template 是一个 WordPress 动作钩子,在加载模板文件之前触发,允许开发者在模板加载前执行自定义代码。

关键要点

  • 这是一个动作钩子,用于在模板文件加载前执行操作。
  • 钩子传递三个参数:$_template_file(模板文件完整路径)、$load_once(是否使用 require_once)和 $args(传递给模板的额外参数)。
  • 钩子通过 do_action() 调用,位于 load_template() 函数中。
  • 首次引入于 WordPress 6.1.0 版本。

代码示例

do_action( 'wp_before_load_template', $_template_file, $load_once, $args );

注意事项

  • 钩子主要用于扩展模板加载过程,例如添加条件逻辑或修改参数。
  • 确保钩子回调函数正确处理参数,以避免影响模板加载。

📄 原文内容

Fires before a template file is loaded.

Parameters

$_template_filestring
The full path to the template file.
$load_oncebool
Whether to require_once or require.
$argsarray
Additional arguments passed to the template.

Source

do_action( 'wp_before_load_template', $_template_file, $load_once, $args );

Changelog

Version Description
6.1.0 Introduced.