【发布时间】:2019-04-09 10:01:42
【问题描述】:
所以我创建了自己的特殊do_parse_request 过滤器,它的效果非常好。拦截正确的请求以返回正确的内容,其他请求继续以它们正常的 WordPressy 方式。基本上,我在劫持某些 URL 以在正常的 WordPress 路由之外做一些自定义的事情。
除了...尽管内容正确,但响应始终是 404。
如果常规请求未完成,我是否缺少需要在 WordPress 中设置的内容?
add_filter('do_parse_request', function($do_parse, $wp) {
//... bunch of code to dynamically determine
//if a page belongs to custom source, which works
if ($ismypage){
remove_action('template_redirect', 'redirect_canonical');
$wp->query_vars['post_type'] = 'my_post_type';
$wp->query_vars['name'] = $mypagename;
return false;
//always a 404, even though I get expected content...
}
return $do_parse;
}
试过了:
四处逛逛,在返回 false 之前查看了 WP_REST_Response。获取:
$testResponse = new WP_REST_Response();
var_dump($testResponse->get_headers());
//empty array
var_dump($testResponse->get_status());
//int 200
在网络上挖掘更多内容后发现了一个文字 status_header 方法。甚至尝试过header("HTTP/1.1 200 Success")
//... right before return false
status_header(200);
//still gets 404 on the page, still gets correct content
header("HTTP/1.1 200 Success");
//same issue
我开始认为这不是我的问题的原因...
【问题讨论】:
标签: php wordpress response-headers