send_headers
云策文档标注
概述
send_headers 是一个 WordPress 动作钩子,在 HTTP 响应头(如缓存、内容类型等)发送后触发,用于向响应中添加自定义头部信息。
关键要点
- send_headers 钩子在 HTTP 响应头发送后执行,允许开发者添加或修改头部。
- 从 WordPress 6.1 起,此钩子触发时间移至 pre_get_posts 之后,支持使用条件标签(如 is_single、is_404)。
- 参数为 $wp(WordPress 环境实例,按引用传递),可通过 do_action_ref_array 调用。
- 常用于添加如 X-UA-Compatible 或 X-Robots-Tag 等自定义 HTTP 头,以优化浏览器兼容性或 SEO 控制。
代码示例
add_action( 'send_headers', 'add_header_xua' );
function add_header_xua() {
header( 'X-UA-Compatible: IE=edge,chrome=1' );
}add_action( 'send_headers', function() {
if ( '0' != get_option( 'blog_public' ) || ( defined( 'WP_DEBUG' ) && true == WP_DEBUG ) ) {
header( 'X-Robots-Tag: noindex, nofollow', true );
}
}, 99 );注意事项
- 使用 header() 函数添加头部时,需确保在输出内容前调用,避免错误。
- 从 WordPress 6.1 开始,钩子触发时机变化,可能影响依赖早期触发的代码,需检查兼容性。
- 示例代码展示了如何条件性设置头部,并可通过优先级参数(如 99)控制执行顺序。
原文内容
Fires once the requested HTTP headers for caching, content type, etc. have been sent.
Description
The ‘wp_finalized_template_enhancement_output_buffer’ action may be used to send headers after rendering the template into an output buffer.
Parameters
$wpWP-
Current WordPress environment instance (passed by reference).
Source
do_action_ref_array( 'send_headers', array( &$this ) );
Changelog
| Version | Description |
|---|---|
| 2.1.0 | Introduced. |
Skip to note 5 content
Daniel Roch
Since WordPress 6.1, the send_headers action has been moved to later in core load. It is now triggered after pre_get_posts.
Conditionals tags can now be used (is_single, is_404 and so on).
More information on this dev-note: https://make.wordpress.org/core/2022/10/10/moving-the-send_headers-action-to-later-in-the-load/
Skip to note 6 content
Collins Mbaka
As an example: HTML5 Boilerplate provides an X-UA-Compatible meta element by default. This element breaks validation but can be moved to a header. Adding the following to functions.php fixes the validation issue and provides IE users a better experience.
add_action( 'send_headers', 'add_header_xua' ); function add_header_xua() { header( 'X-UA-Compatible: IE=edge,chrome=1' ); }Skip to note 7 content
Steven Lin
Example migrated from Codex:
As an example: HTML5 Boilerplate provides an X-UA-Compatible meta element by default. This element breaks validation, but can be moved to a header. Adding the following to functions.php fixes the validation issue and provides IE users a better experience.
add_action( 'send_headers', 'add_header_xua' ); function add_header_xua() { header( 'X-UA-Compatible: IE=edge,chrome=1' ); }Skip to note 8 content
wp_kc
Example of how to conditionally send a header, replacing previously set header, if any…
/** * Send "X-Robots-Tag: noindex, nofollow" header if not a public web site. * If WP_DEBUG is true, treat web site as if it is non-public. */ add_action( 'send_headers', function() { if ( '0' != get_option( 'blog_public' ) || ( defined( 'WP_DEBUG' ) && true == WP_DEBUG ) ) { /** * Tell robots not to index or follow * Set header replace parameter to true */ header( 'X-Robots-Tag: noindex, nofollow', true ); } }, 99 ); // Try to execute last with priority set to 99