Make WordPress Core

#59176 closed defect (bug) (invalid)

functions.php loads more than one time at one refresh

Reported by: skarabeq's profile skarabeq Owned by:
Milestone: Priority: normal
Severity: major Version: 6.3
Component: General Keywords:
Focuses: Cc:

Description

I have added into functions.php the function that adds into wp-content/debug.log text. So if I'm into wp-admin part it store only one time per refresh, but if I'm outside admin It call the functions.php more than one time. You can do it with the following code:

<?php
    function write_log($log) {
        if (true === WP_DEBUG) {
            if (is_array($log) || is_object($log)) {
                error_log(print_r($log, true));
            } else {
                error_log($log);
            }
        }
    }
write_log('Test log: ' . date('Y-m-d H:i:s'));

If I create a custom plugin the situation is the same.

Change History (2)

#1 @skarabeq
19 months ago

  • Version set to 6.3

#2 @johnbillion
19 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from assigned to closed

Thanks for the report @skarabeq.

This is almost certainly caused by multiple requests being sent to your site (for example an Ajax request or an asset that triggers a 404 response), rather than functions.php loading more than once in a single request. If that was the case you'd most likely see a fatal error due to a function redefinition, and the problem would be much more severe and visible.

I recommend that in your write_log() function you add the contents of $_SERVER so you can see the URL path that's requested for each log entry, and then go from there.

I'll close this ticket off as I am confident this is not a bug in the WordPress core software. If you need any further help, please try the WordPress.org support forums or WordPress Development Stack Exchange.

Note: See TracTickets for help on using tickets.