WordPress error_log() writes multiple times

While developing a plugin I tried to dump some data to wp-content/debug.log using error_log() function and noticed that the same data is being written multiple times.

Initially, I though I’m doing smth wrong in my OOP plugin architecture (with namespaces and autoloading), but then I tried doing it from the default theme’s functions.php file, from within the twentyseventeen_setup() function, hooked to after_setup_theme action.

Still, I get 3 same messages in debug log. Then I tried running it through xDebug, with a breakpoint on the twentyseventeen_setup() function. To me, it looks like it’s called 3 times, like some redirect or smth. See this quick screen recording to see what’s happening. Breakpoint is hit 3 times, exactly. Why is that?

Tried loading the URL with HTTPie command line utility to check for redirects, but the server response is 200:

HTTP/1.1 200 OK
Connection: keep-alive
Content-Encoding: gzip
Content-Type: text/html; charset=UTF-8
Date: Fri, 21 Apr 2017 13:32:27 GMT
Link: <http://test.dev/wp-json/>; rel="https://api.w.org/"
Link: <http://test.dev/>; rel=shortlink
Server: nginx/1.11.9
Transfer-Encoding: chunked

For the development environment, I’m using Laravel’s Homestead on macOS. There’s a WPML and Query Monitor plugin installed (however, I tried this on couple different WP installs without plugins and it works the same way).

What am I doing wrong? How to avoid this duplication?

The reason I’m asking is not just debugging convenience, I can totally live with multiplied debug messages. What bothers me is the fact that it looks like WordPress is behaving strangely or my plugin (or theme) is loaded and executed multiple times. The latter, of course, is unacceptable.

Read more here: WordPress error_log() writes multiple times

Leave a Reply

Your email address will not be published. Required fields are marked *