主题开发文档

插件 API 钩子

💡 云策文档标注

概述

本文档介绍了 WordPress 插件 API 中的钩子(Hooks),包括动作(Actions)和过滤器(Filters),并重点说明了主题中需要手动包含的特定钩子,以确保与插件的兼容性。

关键要点

  • 钩子是插件添加功能的核心机制,包括动作和过滤器,大多数钩子由 WordPress 内部执行,无需主题特殊处理。
  • 少数钩子需要在主题模板中显式包含,使用特定的模板标签(Template Tags)来触发,如 wp_head()、wp_body_open()、wp_footer()、wp_meta() 和 comment_form()。
  • 这些钩子的放置位置有具体要求,例如 wp_head() 应位于 header.php 的 元素末尾,wp_footer() 应位于 footer.php 的 标签前。

注意事项

  • 建议参考核心主题的模板文件,以了解这些钩子的实际使用示例。

📄 原文内容

A theme should work well with WordPress plugins. Plugins add functionality by using actions and filters, which are collectively called hooks (see Plugin API for more information).

Most hooks are executed internally by WordPress, so your theme does not need special tags for them to work. However, a few hooks need to be included in your theme templates. These hooks are fired by special Template Tags:

wp_head()
Goes at the end of the <head> element of a theme’s header.php template file.
wp_body_open()
Goes at the begining of the <body> element of a theme’s header.php template file.
wp_footer()
Goes in footer.php, just before the closing </body> tag.
wp_meta()
Typically goes in the <li>Meta</li> section of a Theme’s menu or sidebar.
comment_form()
Goes in comments.php directly before the file’s closing tag (</div>).

Take a look at a core theme’s templates for examples of how these hooks are used.