1

It seems for some reason, on a theme I'm editing, the 404.php template is being executed every page load, even though what was actually being displayed was perfectly normal, whether it be the index, a single post or page, etc.

I was able to determine this by noticing my widgets were reporting they were being called to render twice upon every page load.

The only remedy was to "return" out of the 404.php file right away, so all the code it contains won't be executed needlessly (or delete it outright).

But now my website has no 404 page.

How can I figure out why this is occurring? Like, a stack trace of events that show what triggers a call to the theme's 404 page?


Edit: Okay, after a lot of head scratching I've determined this is being caused by code similar to this:

$post_img_title_html = '<span class="img-container"><img src="http://example.com/images/animationspotlight/' . $thumb_name . '" alt="' . $post_title . '" /></span>';

Basically would result in <span><img src="http://example.com/image.png" alt="Title" /></span>

BUT...in my test environment, this image does not exist. So it's just a typical "broken image" display. No big deal?

WHY...is this triggering a call to WordPress to completely "render" the theme's 404.php file? Again, it's happening "behind the scenes" as I see no indication of the 404 on the page I'm viewing. But I don't want my website to be doing all this extra rendering/processing of the entire 404 page and it's widgets etc, for nothing?...

Is there some kind of "404 priority" or "sensitivity" setting somewhere. I mean, I don't even know how to put it into words it's so basic.

It does have to do with htaccess. As if I delete the file, the 404's don't occur. But of course, permalinks are broken. I re-save permalinks, pages work again, but every "not found" image results in the theme's 404.php being loaded. Gotta be a way around this?

0

I'm not totally on track with the issue because I don't have enough information, or any troubleshooting that you've done beyond what appears to be happening from a user-standpoint, but I'm going to give you my best shot here.

In any situation where there are 404 issues, always try to: (1) Reset the permalinks from your WP Dashboard. (2) Try to reset your .htaccess file.

A quick Google of either of those troubleshooting steps should yield a guide on how to accomplish each.

If neither of those two troubleshooting steps accomplish resolve to the issue you're experiencing, then I recommend you dive a little deeper. To begin troubleshooting this issue further than we have, we are going to need more information so let's try the following:

Step 1.

Enable wp_debug (https://wordpress.org/support/article/debugging-in-wordpress/) so that we can see if there are any blaring PHP issues on the pages you're viewing.

I would also try to find out which template is serving the current page that you're looking at when you see the issue with all widgets and plugins enabled as they are when you notice the problem. This will take the addition of some code on your part. There's a great summary on how to achieve this in the following link:

How do you find out which template page is serving the current page?

Step 2.

Then, I would disable all widgets and plugins; Go back to the problem-pages you identified in Step 1 (above), and see if the issue still exists. If so, identify which templates are serving these pages. (Notice whether there is a difference in the templates serving the problem-pages now that the widgets and plugins are disabled).

Work your way through the code of the template that's been identified in each scenario and hopefully this will assist you in identifying where the problem is.

This is not a total solution, but at least a path to get you started. Add more information about what you've found from the steps above, and I'll be happy to assist further. Good luck, and I hope this helps!

| improve this answer | |
  • Thanks for all that. So long story short I've narrowed it down to two calls both activating the 404.php template. Number 1) is the_content() and 2) dynamic_sidebar('sidebar-1'). These both cause the "404.php" template to be loaded, but not DISPLAYED. I simply get log outputs if I put a print statement in the 404.php file showing that it's being loaded. Otherwise, everything is fine on the site. I can't figure out what is causing, for example, a simple call to the_content() on single.php to also load the 404.php template? – FTLRalph Apr 13 at 23:12
  • Hi, can you include the code that you're seeing in these affected template(s)? – Jan Michael Wallace Apr 13 at 23:56
  • I'm not entirely certain what you're looking for, thanks but I've narrowed it down to basically the page trying to load standard IMG elements in HTML that doesn't exist. No big deal but...it's triggering a call to load the theme's 404.php? Why would that be? – FTLRalph Apr 14 at 16:37
  • The issue you’re running into is similar the the following thread. I won’t be able to offer a definitive answer about your issue with just the information you’ve given. Debugging thoroughly for a total understanding is necessary in every aspect of development. Try reading through the following link I’m pointing you towards and you should find yourself with another possible way to resolve your issue: wordpress.stackexchange.com/questions/292921/… – Jan Michael Wallace Apr 14 at 20:21

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.