Page stats for administrators
Administrators see how long the page took to build, how many database queries it ran and how much memory it used, in the admin bar.
<?php
add_action(
'admin_bar_menu',
static function ( $bar ) {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$bar->add_node(
array(
'id' => 'snipfire-stats',
'parent' => 'top-secondary',
'title' => '<span id="snipfire-stats">…</span>',
)
);
},
200
);
// Filled in at the very end, when the numbers are final.
$snipfire_stats = static function () {
if ( ! current_user_can( 'manage_options' ) || ! is_admin_bar_showing() ) {
return;
}
$text = sprintf(
'%s s · %d queries · %s MB',
number_format_i18n( timer_float(), 2 ),
get_num_queries(),
number_format_i18n( memory_get_peak_usage() / MB_IN_BYTES, 1 )
);
echo '<script>( function () { var el = document.getElementById( "snipfire-stats" ); if ( el ) { el.textContent = ' . wp_json_encode( $text ) . '; } } )();</script>';
};
add_action( 'wp_footer', $snipfire_stats, 9999 );
add_action( 'admin_footer', $snipfire_stats, 9999 );
In these packs
- Developer tools: Maintenance mode, template names, page stats, environment labels and a WP-Cron viewer.