pre_handle_404
云策文档标注
概述
pre_handle_404 是一个 WordPress 过滤器,用于控制是否绕过默认的 404 状态处理。开发者可以通过此过滤器提前中断处理流程。
关键要点
- 过滤器名称:pre_handle_404
- 作用:允许在 WP::handle_404() 中提前返回,避免执行默认的 404 处理逻辑
- 参数:$preempt(布尔值,默认 false)和 $wp_query(WP_Query 对象)
- 返回值:如果过滤器返回非 false 值,将中断处理并提前返回
- 引入版本:WordPress 4.5.0
代码示例
if ( false !== apply_filters( 'pre_handle_404', false, $wp_query ) ) {
// 处理逻辑
}注意事项
- 此过滤器主要用于自定义 404 处理逻辑,例如在特定条件下避免显示 404 页面
- 确保正确处理 $wp_query 参数,以避免影响其他查询功能
原文内容
Filters whether to short-circuit default header status handling.
Description
Returning a non-false value from the filter will short-circuit the handling and return early.
Parameters
$preemptbool-
Whether to short-circuit default header status handling. Default false.
$wp_queryWP_Query-
WordPress Query object.
Source
if ( false !== apply_filters( 'pre_handle_404', false, $wp_query ) ) {
Changelog
| Version | Description |
|---|---|
| 4.5.0 | Introduced. |