wp_footer
云策文档标注
概述
wp_footer 是一个 WordPress 动作钩子,用于在前端页面关闭 body 标签前输出脚本或数据。它由 wp_footer() 函数触发,是主题开发中的核心钩子,但依赖于主题是否包含。
关键要点
- wp_footer 是一个动作钩子,作为事件触发器,而非内容过滤器。
- 此钩子不提供参数,函数应直接输出到浏览器或执行后台任务,不应返回值或接受参数。
- 钩子的可用性取决于主题作者是否包含 wp_footer() 调用,并非所有主题都支持。
- 默认输出包括管理面板,通常位于主题顶部,但建议保持在页脚以兼容插件脚本绑定。
- 使用 add_action('wp_footer', 'your_function') 添加自定义函数,可通过优先级参数控制执行顺序。
代码示例
function your_function() {
echo 'This is inserted at the bottom';
}
add_action( 'wp_footer', 'your_function' );注意事项
- 在 WordPress 5.8 版本中,wp_footer 可能在新的基于块的 Widgets 屏幕上每个小部件后调用,建议添加条件限制。
- 脚本排队(如 enqueue_script)通常在优先级 20 执行,可通过调整优先级控制钩子执行顺序。
原文内容
Prints scripts or data before the closing body tag on the front end.
Source
do_action( 'wp_footer' );
Changelog
| Version | Description |
|---|---|
| 1.5.1 | Introduced. |
Skip to note 5 content
1naveengiri
Example code:
function your_function() { echo 'This is inserted at the bottom'; } add_action( 'wp_footer', 'your_function' );Skip to note 6 content
dubleynguyen
In version 5.8 of wordpress the action do_action(‘wp_footer’) seems to be called after each widget on the new block based Widgets screen. I think add a condition in your function to limit this.
Skip to note 7 content
Steven Lin
Example migrated from Codex:
If you want to influence the moment that your hook is executed, you can append an integer as 3rd argument to
add_action:This is inserted at the bottom</p>'; } add_action( 'wp_footer', 'your_function', 100 ); ?>The higher the number, the lower the priority and as a result your hook will be executed further down the page. Enqueued scripts are executed at priority level 20.
Skip to note 8 content
Steven Lin
Example migrated from Codex:
To load jQuery you need to use enqueue function. As described enqueue script executed at priority level 20.