Performance PHP Intermediate

Log Page Loads Slower Than 2 Seconds

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.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 16 lines
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 );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

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.

How it works

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.

Things to watch

  • The log is wherever PHP writes errors: your host’s error log, or wp-content/debug.log if WP_DEBUG_LOG is on.
  • Admin pages and AJAX requests are logged too, which helps find slow plugin screens.
  • Use Query Monitor on a slow URL to see which queries or hooks are responsible.

How to test it

Temporarily change 2.0 to 0.01, load a page, and check the log for a “[slow page]” line. Then change it back.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only