平常在维护WordPress的时候想要看看每个页面的加载时间、内存占用量等数据都很不方便,下面我们来说下如何使用WordPress显示数据库查询次数、内存占用及加载时间。

下面给大家整理的这种方法,这种是通过代码直接生成的。

将一下代码添加到主题functions.php文件的最后面:

仅管理员可见

add_action( 'wp_footer', function () {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$stat = sprintf( '本页生成数据库 %d 次查询,耗时 %.3f 秒,使用 %.2fMB 内存',
get_num_queries(),
timer_stop( 0, 3 ),
memory_get_peak_usage() / 1024 / 1024
);
echo "<center>{$stat}</center>";
} );

任何人可见

add_action( 'wp_footer', function () {
$stat = sprintf( '本页生成数据库 %d 次查询,耗时 %.3f 秒,使用 %.2fMB 内存',
get_num_queries(),
timer_stop( 0, 3 ),
memory_get_peak_usage() / 1024 / 1024
);
echo "<center>{$stat}</center>";
} );