Server-Timing Header for Administrators
Adds a Server-Timing header with PHP time, database query count and peak memory to pages viewed by administrators, so you can read the numbers in your browser's developer tools Network tab.
Opens WPIDE on
Code
$wpide_server_timing = function () {
if ( headers_sent() || ! is_user_logged_in() || ! current_user_can( 'manage_options' ) ) {
return;
}
header( sprintf(
'Server-Timing: php;dur=%.1f;desc="PHP until the template starts", db;desc="%d queries", mem;desc="%s peak memory"',
timer_float() * 1000,
get_num_queries(),
size_format( memory_get_peak_usage( true ), 1 )
) );
};
// Sent just before the page template (or admin screen) starts rendering.
add_action( 'template_redirect', $wpide_server_timing, PHP_INT_MAX );
add_action( 'admin_init', $wpide_server_timing, PHP_INT_MAX );
Notes
Visitors never receive the header; the time covers everything WordPress does before the page template starts rendering.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.