Skip to content

Instantly share code, notes, and snippets.

@thefuxia
Created February 27, 2013 13:17
Show Gist options
  • Save thefuxia/5047821 to your computer and use it in GitHub Desktop.
Save thefuxia/5047821 to your computer and use it in GitHub Desktop.
T5 Inspect Queries Adds a list of all queries at the end of each file.
<?php
/**
* Plugin Name: T5 Inspect Queries
* Description: Adds a list of all queries at the end of each file.
*
* Add the following to your wp-config.php:
define( 'WP_DEBUG', TRUE );
define( 'SAVEQUERIES', TRUE );
*/
add_action( 'shutdown', 't5_inspect_queries' );
/**
* Print a list of all database queries.
*
* @wp-hook shutdown
* @return void
*/
function t5_inspect_queries()
{
global $wpdb;
$list = '';
if ( ! empty( $wpdb->queries ) )
{
$queries = array ();
foreach ( $wpdb->queries as $query )
{
$queries[] = sprintf(
'<li><pre>%1$s</pre>Time: %2$s sec<pre>%3$s</pre></li>',
nl2br( esc_html( $query[0] ) ),
number_format( sprintf('%0.1f', $query[1] * 1000), 1, '.', ',' ),
esc_html( implode( "\n", explode(', ', $query[2] ) ) )
);
}
$list = '<ol>' . implode( '', $queries ) . '</ol>';
}
printf(
'<style>pre{white-space:pre-wrap !important}</style>
<div class="%1$s"><p><b>%2$s Queries</b></p>%3$s</div>',
__FUNCTION__,
$wpdb->num_queries,
$list
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment