Display Query Count, Load Time, and Memory Usage in WordPress Footer
When developing or debugging WordPress performance, it's useful to know the number of database queries, page load time, and memory usage. The following methods allow you to display this information in the page footer or HTML source code.
1. Add the Performance Statistics Function
Add the following code to your current theme's functions.php file:
// Display page query count, load time, and memory usage
function performance( $visible = false ) {
$stat = sprintf(
'%d queries in %.3f seconds, using %.2fMB memory',
get_num_queries(),
timer_stop( 0, 3 ),
memory_get_peak_usage() / 1024 / 1024
);
echo $visible ? $stat : "";
}
This function calculates:
- Database Query Count: via
get_num_queries(). - Page Load Time: via
timer_stop(0, 3)(seconds, 3 decimal places). - Peak Memory Usage: via
memory_get_peak_usage()converted to MB.
2. Call the Performance Function
Call the function where you want it displayed (e.g., in a theme template file):
<?php if ( function_exists('performance') ) performance( false ); ?>
The performance() function parameter:
false(default): Statistics are not displayed on the front‑end but output as an HTML comment in the source code, useful for debugging without affecting appearance.true: Statistics are directly displayed on the front‑end.
3. Automatically Load in Footer (Recommended)
To automatically show statistics in the footer, add this to functions.php (ensure your theme template, e.g., footer.php, includes <?php wp_footer(); ?>):
add_action( 'wp_footer', 'performance', 20 );
This hooks the performance() function to WordPress's wp_footer action with priority 20. By default, it's called with false, outputting the statistics as an HTML comment.
4. Customize the Display
To show statistics directly in the footer, modify the hook:
// Display statistics directly in the footer
add_action( 'wp_footer', function() {
if ( function_exists('performance') ) {
performance( true ); // Set parameter to true
}
}, 20 );
Important Notes
- This feature is primarily for development and debugging. On production sites, remove it or keep it as a comment to avoid exposing server information.
- Ensure your theme supports the
wp_footer()hook; otherwise, footer loading won't work. - The
memory_get_peak_usage()function requires PHP 5.2+, which modern WordPress environments typically meet. - The memory usage reported is the peak memory during PHP script execution, in MB.
Using these methods, you can easily monitor WordPress page performance metrics for optimization.