admin管理员组文章数量:1390204
Working on a plugin to redirect 404 to a different page. Here's the problem. I occasionally get this in the log file:
[08-Apr-2020 14:48:45 UTC] PHP Notice: Trying to get property of non-object in {path}/wp-includes/class-wp-query.php on line 3998
[08-Apr-2020 14:48:45 UTC] PHP Notice: Trying to get property of non-object in {path}/wp-includes/class-wp-query.php on line 4000
[08-Apr-2020 14:48:45 UTC] PHP Notice: Trying to get property of non-object in {path}/wp-includes/class-wp-query.php on line 4002
which is apparently related to this bug:
I've been doing this:
add_action('template_redirect', array($this, 'My_404_Handler'));
And I've been calling is_404() in that function. From the bug report, whether it returns a true or false depends on when you call it. In my case the wp-includes/class-wp-query.php has a function get_queried_object() which can return a null value to a function is_page(). The problem, is_page() only handles object results and produces the error above when get_queried_object returns a null.
I've also looked over the server log and can't pinpoint any specific url requests that's causing this error.
I'm hoping someone has some experience with this and can tell me if changing to the code below is going to fix the problem?
add_action('wp', array($this, 'My_404_Handler'));
I'm testing but since I don't know how to reproduce the error, I'm just taking a shot in the dark.
Thanks
edit
404 function
public function My_Plugin_handle404() {
if (is_404()) {
//user set option to show the real 404
if (get_option('Show404') == 'TRUE') {
include(get_404_template());
} else {
//log the request info to the db and show a blank page with a 200 result
$this->My_Plugin_Process_Request();
status_header(200);
exit;
}
}
}
Working on a plugin to redirect 404 to a different page. Here's the problem. I occasionally get this in the log file:
[08-Apr-2020 14:48:45 UTC] PHP Notice: Trying to get property of non-object in {path}/wp-includes/class-wp-query.php on line 3998
[08-Apr-2020 14:48:45 UTC] PHP Notice: Trying to get property of non-object in {path}/wp-includes/class-wp-query.php on line 4000
[08-Apr-2020 14:48:45 UTC] PHP Notice: Trying to get property of non-object in {path}/wp-includes/class-wp-query.php on line 4002
which is apparently related to this bug:
https://core.trac.wordpress/ticket/29660
I've been doing this:
add_action('template_redirect', array($this, 'My_404_Handler'));
And I've been calling is_404() in that function. From the bug report, whether it returns a true or false depends on when you call it. In my case the wp-includes/class-wp-query.php has a function get_queried_object() which can return a null value to a function is_page(). The problem, is_page() only handles object results and produces the error above when get_queried_object returns a null.
I've also looked over the server log and can't pinpoint any specific url requests that's causing this error.
I'm hoping someone has some experience with this and can tell me if changing to the code below is going to fix the problem?
add_action('wp', array($this, 'My_404_Handler'));
I'm testing but since I don't know how to reproduce the error, I'm just taking a shot in the dark.
Thanks
edit
404 function
public function My_Plugin_handle404() {
if (is_404()) {
//user set option to show the real 404
if (get_option('Show404') == 'TRUE') {
include(get_404_template());
} else {
//log the request info to the db and show a blank page with a 200 result
$this->My_Plugin_Process_Request();
status_header(200);
exit;
}
}
}
Share
Improve this question
edited Apr 8, 2020 at 18:42
uPrompt
asked Apr 8, 2020 at 17:13
uPromptuPrompt
1729 bronze badges
4
- Why not use htaccess to handle 404's? Simple to do. Or you could have your plugin write to the htaccess, and let the user select the 404 page from a drop-down list of pages. – Rick Hellewell Commented Apr 8, 2020 at 18:02
- Hadn't thought of that and an idea to toy with. In the future though I want the option to redirect 404's based on the incoming request. Think honeypot. It's a research site. I want those probing the site for files or directories to find what they expect to find. – uPrompt Commented Apr 8, 2020 at 18:19
- @RickHellewell That's a very weird suggestion. What makes you think there is a .htaccess, and why should it be writable? – fuxia ♦ Commented Apr 8, 2020 at 18:23
- @fuxia I just suggested an htaccess as a way to 404, not having any details of why he needed a custom 404. And I made it a comment, rather than an answer (since my comment didn't directly access his question). And it is easy enough to have a command in a plugin to write to htaccess using the insert_with_markers() function (which is how WP does it). It is a valid way to modify htaccess - I use it in one of my plugins. – Rick Hellewell Commented Apr 8, 2020 at 20:33
1 Answer
Reset to default 0The proper hook to handle custom 404 content is 404_template
. This happens when WordPress is trying to include the default template, but before any HTTP headers have been sent.
So you can change the template, send or overwrite HTTP headers, and all of that without any checks, because WordPress has done that for you already.
In most cases you should not redirect those requests. A redirect is a 3xx status, not a 4xx.
You might find some inspiration in my old 404 plugin.
本文标签: 404 errorPluginProper way to handle 404 pages
版权声明:本文标题:404 error - Plugin - Proper way to handle 404 pages? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744585389a2614164.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论