wp_footer()
云策文档标注
概述
wp_footer() 是一个 WordPress 函数,用于触发 wp_footer 动作钩子,在前端页面关闭 body 标签前输出脚本或数据。开发者可通过此钩子添加自定义代码。
关键要点
- wp_footer() 函数调用 do_action('wp_footer'),触发 wp_footer 动作钩子。
- 该钩子用于在前端页面 body 标签关闭前打印脚本或数据,常用于添加 JavaScript 代码或分析工具。
- wp_footer 钩子自 WordPress 1.5.1 版本引入,是核心功能的一部分。
- 相关函数包括 do_action(),用于调用添加到动作钩子的回调函数。
代码示例
function wpdocs_js_code_example() {
?>
<script>
// 添加自定义 JavaScript 代码
console.log('Footer script loaded');
</script>
<?php
}
add_action('wp_footer', 'wpdocs_js_code_example');注意事项
确保在主题的 footer.php 文件中调用 wp_footer() 函数,否则钩子可能无法触发。自定义代码应通过 add_action 添加到 wp_footer 钩子,避免直接修改核心文件。
原文内容
Fires the wp_footer action.
Description
See ‘wp_footer’.
Source
function wp_footer() {
/**
* Prints scripts or data before the closing body tag on the front end.
*
* @since 1.5.1
*/
do_action( 'wp_footer' );
}
Hooks
- do_action( ‘wp_footer’ )
-
Prints scripts or data before the closing body tag on the front end.
Changelog
| Version | Description |
|---|---|
| 1.5.1 | Introduced. |
Skip to note 3 content
hearvox
Example:
<body> <!-- All the document's HTML goes first. --> <!-- Then last, before closing the body tag, add: --> </body>Skip to note 4 content
thejaydip
You can add javascript code to your site’s footer