WordPress Theme files are run twice, bugging my hooks

@Webtrac You could modify your logged output to see the request path that invoked your functions.php the second time:

-error_log('test');
+error_log('Request path: ' . $_SERVER['REQUEST_URI']);

If I do that and load the homepage, I see two requests just as you reported:

But it’s not Local causing the second request — it’s the browser making a request for the default favicon. If you don’t have a favicion.ico file in your site root, the request goes through WordPress’s routing system which invokes active theme (and plugin) code along the way.

You can fix it by either:

  • Dropping a favicon.ico in your site root locally (in your Local site’s app/public folder). When you do this your browser finds that file and PHP is not invoked the second time.
  • Setting a “Site Icon” at Appearance → Customize → Site Identity → Site Icon if your active theme supports this. (You may have to remove your debug call before the Customizer will load.) When you do this WP adds a <link rel="icon" …> to the HTML head that your browser will use instead of looking for the default favicon, so again the second request doesn’t involve PHP.

When I make either of those changes I see only one logged entry per page refresh:

> touch wp-content/debug.log && echo '' > wp-content/debug.log && tail -f wp-content/debug.log

[17-Mar-2025 14:31:08 UTC] Request path: /

You might be seeing this behaviour locally but not on your production server if your production server has a favicon.ico in the root (or a Site Icon set) but your local version doesn’t.

If you’re experimenting with this and you add a favicon.ico, load the page, then remove the favicon.ico file, note that your browser will still cache the favicon for some time. You’ll likely still see only one request even if you delete the favicon from disk. So it’s best to test this with private browser windows.