Write a line to the PHP error log whenever WordPress takes more than 2 seconds to build a page, with the URL, query count and memory.
add_action( 'shutdown', function (): void {
$seconds = timer_float();
if ( $seconds < 2.0 ) {
return;
}
error_log( sprintf(
'[slow page] %.2fs %s %s | %d queries | %s MB | user %d',
$seconds,
sanitize_text_field( $_SERVER['REQUEST_METHOD'] ?? '' ),
esc_url_raw( $_SERVER['REQUEST_URI'] ?? '' ),
get_num_queries(),
number_format( memory_get_peak_usage() / 1048576, 1 ),
get_current_user_id()
) );
}, 999 );
Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.
Slow pages are often slow only sometimes, or only for certain URLs, which makes them hard to catch. This snippet quietly logs every request that takes longer than two seconds on the server, so you can see patterns.
WordPress starts a timer at the very beginning of each request. On shutdown, the last hook to run, timer_float() gives the total time. If it’s over two seconds, the snippet writes the time, method, URL, database query count, peak memory and user id to the PHP error log with error_log().
This measures server time only, not how long the page takes to appear in the browser.
WP_DEBUG_LOG is on.Temporarily change 2.0 to 0.01, load a page, and check the log for a “[slow page]” line. Then change it back.