添加…
define( \'SAVEQUERIES\', TRUE );
…到您的
wp-config.php
, 和检查
$wpdb->queries
在
shutdown
. 这是最新的钩子和
only 在此之后不会激发任何查询。此外,它还可以
wp-admin/
也
插件示例代码:
<?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
);
}
在考虑了很久之后,我编写了另一个更适合我的需要的插件,如果你喜欢控制台的话,也可能是你的插件。
<?php
/**
* Plugin Name: T5 Log Queries
* Description: Writes all queries to \'/query-log.sql\'.
* Plugin URI: http://wordpress.stackexchange.com/a/70853/73
* Version: 2012.11.04
* Licence: MIT
*/
add_filter( \'query\', \'t5_log_queries\' );
/**
* Write the SQL to a file.
*
* @wp-hook query
* @param string $query
* @return string Unchanged query
*/
function t5_log_queries( $query )
{
static $first = TRUE;
// Change the path here.
$log_path = apply_filters(
\'t5_log_queries_path\',
ABSPATH . \'query-log.sql\'
);
$header = \'\';
if ( $first )
{
$time = date( \'Y-m-d H:i:s\' );
$request = $_SERVER[\'REQUEST_URI\'];
$header = "\\n\\n# -- Request URI: $request, Time: $time ------------\\n";
$first = FALSE;
}
file_put_contents( $log_path, "$header\\n$query", FILE_APPEND | LOCK_EX );
return $query;
}
使用跟踪文件
tail
(可用
on Windows if Git is installed):
$ tail -f query-log.sql -n 50