WordPress Page Load Time Shortcode

  • follow us in feedly
Published March 1, 2013 by Brad Knutson

It may be beneficial for developers to know the page load time of given pages on their WordPress site. There are also legitimate applications of this on production sites. Perhaps you just want to show off?

To give the developer (or perhaps the user) a little more data, lets also display the number of queries it took to generate the given page. To do this, we’ll utilize the WordPress function get_num_queries(). This useful function gets the number of queries required to generate the current page. We’ll next use the built-in function timer_stop() to determine how long the page took to load.

Add the below shortcode function to your functions.php file in your theme directory.

Like other WordPress functions, to utilize this in a post, enter the [queries].

The Result

50 queries in 0.150 seconds.

Let’s see if we can expand on this a little and get more detailed information. Can we get the amount of time spent on processing the script (PHP) versus actually running the queries (MySQL)?

In order to calculate this, we’ll need to loop through each query and sum the time spent on all of them.

function page_load_stats_shortcode() {
	global $wpdb;
	$total = timer_stop( false, 22 );
	$mysql_time = 0;
	foreach ( $wpdb->queries as $query )
		$mysql_time = $mysql_time + $query[1];
	$php_time = $total - $mysql_time;
	$mysql_per = number_format_i18n( $mysql_time / $total * 100, 2 );
	$php_per   = number_format_i18n( $php_time / $total * 100, 2 );
	$result = 'Page generated in ' . number_format_i18n( $total, 3 ) . ' seconds ( '. $php_per .'% PHP, '. $mysql_per .'% MySQL )';
	return $result;
add_shortcode('page_load_stats', 'page_load_stats_shortcode');

The Result

Page generated in 0.150 seconds ( 98.00% PHP, 2.00% MySQL )

You’ll notice that even though both shortcodes run as separate functions, the equated page load time is the same for both. This is how we know it’s accurate.

Disclaimer: I have WP_Super_Cache running, so the number of queries, page load time and percentages may not be 100% accurate from you, but they are generated from the shortcode.

The following two tabs change content below.
Founder at Inbounderish
Brad Knutson is a Web Developer in the Twin Cities area of Minnesota. He has experience working with WordPress and Drupal, and also has an interest in SEO and Inbound Marketing.

Keep Up-to-Date



See a complete list of topics discussed in blog posts here.

Check These Out

Get 2 Weeks Free! Sign Up Today! Premium Managed WordPress Hosting Genesis Framework for WordPress SEO is complex. Tools should be simple. Thesis Theme for WordPress:  Options Galore and a Helpful Support Community

Share Your Thoughts

Your email address will not be shown.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">