How to Show the Number of Queries and Page Load Time in WordPress

Home » Snippets » How to Show the Number of Queries and Page Load Time in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Have you ever wondered how many queries your WordPress website makes for a single request, or how much time it takes for a page to load? Knowing these metrics is crucial for optimizing your website, improving its performance and ensuring a seamless user experience. In this article, we will guide you on how to display the number of queries and the page load time in WordPress. This clear visibility can give you valuable insights about where improvements can be made.

					function wpturbo_display_query_count_and_time() {
    if (current_user_can('administrator')) {
        $queries_count = get_num_queries();
        $load_time = timer_stop();
        echo '<p>Number of Queries: '.$queries_count.'</p>';
        echo '<p>Page Load Time: '.$load_time.'</p>';
    }
}
add_action('wp_footer', 'wpturbo_display_query_count_and_time');
				

In this code snippet, we’re creating a function named wpturbo_display_query_count_and_time() to display the number of SQL queries Wordpress made to render the page and the total page load time in the footer.

Here’s how it works.

The wpturbo_display_query_count_and_time() function first checks if the current user is an administrator on the Wordpress site. The current_user_can('administrator') function is a WordPress function that checks the current logged-in user’s role and returns a boolean value. It will return ‘True’ if the user has the administrator role and ‘False’ otherwise. We wrap the whole function in this condition to ensure only administrators can view these stats, as they are crucial for performance tuning and not suitable for regular site visitors.

if (current_user_can('administrator')) {
    ...
}

Once inside of the conditional block we make use of two WordPress built-in functions: get_num_queries() and timer_stop(). get_num_queries() returns the total number of SQL queries the Wordpress system ran to render the current page. timer_stop() returns the time in seconds since WordPress completed its loading process – in other words, the time taken to fully load the current page.

$queries_count = get_num_queries();
$load_time = timer_stop();

The results are then echoed or printed out in ‘p’ HTML tags. The ‘.’ operator in PHP is used to concatenate or join the text strings and the variable content.

echo '<p>Number of Queries: '.$queries_count.'</p>';
echo '<p>Page Load Time: '.$load_time.'</p>';

The last line of the code snippet says add_action('wp_footer', 'wpturbo_display_query_count_and_time');. This line hooks wpturbo_display_query_count_and_time() function into the wp_footer action. wp_footer is a Wordpress action that is activated just before the </body> tag in a Wordpress theme.

With this action setup, our wpturbo_display_query_count_and_time() function is going to be called every time the wp_footer action is triggered in a Wordpress page, effectively adding our custom query counter and timer into every page footer on the website – but again, visible only to logged-in administrators.

Register an account to save your snippets or go Pro to get more features.