WordPress Theme files are run twice, bugging my hooks

What issue or error are you experiencing?

I’m building a new WP theme, but when working with redirects and hooks I encountered a bug where my functions were running twice, first i thought i made a mistake, so i turned off all the theme files, only leaving style.css and functions.php, with inside the functions php the following code: error_log(‘test’);

Now this error is logged multiple times every refresh, ive installed the same theme in one of my online sites, there the error log is only run once, so the problem has to be localwp.

How do i fix this?


What steps can be taken to replicate the issue? Feel free to include screenshots, videos, etc

add a blank wp theme with only a functions.php where the only code is a error log and see how many times its running every single refresh


System Details

  • Local Version:
    latest version on 11/03/2025

  • Operating System (OS) and OS version:
    Windows 11


Local Logs

Attach your Local Logs here (Help Doc - Retrieving Local’s Log)
local-logs1.zip (57.9 KB)


Security Reminder
Local does a pretty good job of scrubbing private info from the logs and the errors it produces, however there’s always the possibility that something private can come through. Because these are public forums, always review the screenshots you are sharing to make sure there isn’t private info like passwords being displayed.

Hi @Webtrac!

  • If you open your browser’s Network tab (in Developer Tools) and refresh the page, are there any duplicate requests being sent?

  • Another thing to check could be any additional wp-cron.php requests in the background. Try adding this to your wp-config.php to disable WP-Cron and see if that changes the behavior.

  • Do you have any other plugins running on this site? It might also be worth disabling everything aside from the theme just to see if that helps.

Hi Nick,

Ive disabled wp cron in the wp config file but that didnt change it, in my network tab the only request i see twice is https://secure.gravatar.com/avatar/729ae85bf62b9917e93538db2f2688ca?s=26&d=mm&r=g

As far on the other plugins, only ACF pro and facetwp, and i have these in every site running, again the excact copy of the site thats on one of my online domains doesnt have this problem!

Where is your redirection logic being set up?

Does the issue persist if you utilize a different browser or flush caches?

Ive tested it in a different browser, same problem there

I also tried installing another team and put an error log in that function.php file, still the same.

As far as the redirects, i havent done anything other than a clean wp install, the only redirects i know of is in the htacces file:

BEGIN WordPress

The directives (lines) between “BEGIN WordPress” and “END WordPress” are

dynamically generated, and should only be modified via WordPress filters.

Any changes to the directives between these markers will be overwritten.

RewriteEngine On

RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

RewriteBase /

RewriteRule ^index.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . /index.php [L]

END WordPress

What Router Mode have you been using primarily?
Have you tried securing the site with HTTPS using Site Domains and trusting the SSL? Or switching to Localhost to force HTTP?

Hi Nick,

It’s on site domains, but on your advice i tried changing it to localhost but the same problem persist ):

ive also been debugging with chatgpt for hours, and i cant pinpoint the problem, if you have a own localwp site running and y add ann debug log in your functions.php its not ran twice?

Kind regards,

Simon

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

1 Like