如标题所示,WordPress网站若想显示数据库查询次数,内存占用,页面加载时间,我们可以尝试如下方法,一种是管理员可见,另一种是所有人可见,看看追格主题小编如何操作的。
1、所有人可见
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>"; } );
2、管理员可见
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>"; } );
上述代码,添加到当前 WordPress主题的functions.php文件中即可。
THE END
评论