0

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.org/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;
        }
    } 
}
  • 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 Apr 8 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 Apr 8 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 Apr 8 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 Apr 8 at 20:33
0

The 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.

| improve this answer | |
  • Ok, I added an edit to the OP with the function I use to handle the 404. Will that still work with the hook you suggest? – uPrompt Apr 8 at 18:49
  • @uPrompt Sure, why not? Try it. And please never send a 2xx status code for a 404 request. – fuxia Apr 8 at 20:43
  • There's a reason for it. Read my comment below the OP. And 404_template was too late in the startup sequence for my purposes otherwise good idea. Tried to hook init and wp_loaded and I'm guessing the is_404 hasn't been set yet as it displays a proper 404. Ended up hooking into wp. So far, no log errors. Thanks. – uPrompt Apr 9 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.