钩子文档

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.

More Information

The wp_footer action is triggered near the tag of the user’s template by the wp_footer() function. Although this is theme-dependent, it is one of the most essential theme hooks, so it is fairly widely supported.

This hook provides no parameters. You use this hook by having your function echo output to the browser, or by having it perform background tasks. Your functions shouldn’t return, and shouldn’t take any parameters.

This hook is theme-dependent which means that it is up to the author of each WordPress theme to include it. It may not be available on all themes, so you should take this into account when using it.

When included, the default output for this function is the admin panel which will be shown in the top of the theme. It should be kept in the footer for every theme because most of the plugin bind their script files or functions to this hook.

This hook is an action which means that it primarily acts as an event trigger, instead of a content filter. This is a semantic difference, but it will help you to remember what this hook does

Source

do_action( 'wp_footer' );

Changelog

Version Description
1.5.1 Introduced.

User Contributed Notes

  1. Skip to note 7 content

    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.