Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.