Use WordPress Debug Stats to Trim the Fat from Your Blog

Word­Press, out of the box with­out any cus­tomiza­tions, is a pret­ty lean blog­ging engine. Pages load swift­ly, and servers hum right along, dish­ing out page after page of deli­cious con­tent to your users.

But who wants to use Word­Press out of the box? More often than not, you’re going to add plu­g­ins, select a bet­ter theme, and so on.

And why should­n’t you? Cus­tomiza­tions set your site apart from the pack, which is what you want!

These cus­tomiza­tions come with a price, how­ev­er: more code requires more time to process that code. And for many plu­g­ins and a grow­ing num­ber of Word­Press themes, there may also be more data­base queries. These can take even longer to process!

All of this pro­cess­ing time means that your users will have to wait that much longer for your con­tent to even begin to load. 

How do you know how many queries are being exe­cut­ed when a page is accessed on your blog? If you’re using most themes, you sim­ply need to view the source of var­i­ous pages of your site. Near the bot­tom, in your foot­er area, you’ll see some­thing like this:

<!-- 25 queries. 0.916 seconds. -->

Nifty, huh? That lit­tle snip­pet can usu­al­ly be found in your the­me’s footer.php file, and it looks like this before it is processed:

<!-- <?php echo get_num_queries(); ?> queries. <?php timer_stop(1); ?> seconds. -->

The num­ber of queries tells you how many times Word­Press had to inter­act with your data­base in order to get the required data to pro­duce your page. The time tells you how long it took your page to be cre­at­ed by the PHP engine, not how long it took to download.

I won’t pre­tend to be able to tell you what “good results” are. Is 17 queries good? Is 34 bad?

The truth is, every Web­site is going to have dif­fer­ent oper­at­ing tol­er­ances because there are thou­sands of dif­fer­ent hosts using hun­dreds of thou­sands of dif­fer­ent serv­er con­fig­u­ra­tions. What might be “high load” on one serv­er might not even cause anoth­er serv­er to sweat.

So how can you use these numbers?

Pri­mar­i­ly, they are an excel­lent indi­ca­tion of how your plu­g­ins are per­form­ing. For exam­ple, with­out much effort, you could prob­a­bly track down half a dozen plu­g­ins which list relat­ed posts for the cur­rent­ly viewed post. It requires a bit of time, but with the debug data in your foot­er, you could try out all of the plu­g­ins and find the one which adds the least amount of pro­cess­ing over­head to your site.

If you’re able to add func­tion­al­i­ty while still main­tain­ing a speedy site, your read­ers will be appre­cia­tive, and they’ll be less like­ly to click away from your site due to long load times.

Remem­ber: It does­n’t mat­ter how fast a user’s Inter­net con­nec­tion is, if a site takes a long time to process, it’s still going to be slow!

Now, I men­tioned the The­sis theme ear­li­er, but if you’re using it, how would you go about adding this debug info to your footer?

Here’s a lit­tle snip­pet of code you can add to your custom/custom_functions.php file which will out­put the debug info for the admin user only. This ver­sion of the code also omits the com­ment tags so that you don’t have to view the source of your page to see how it’s per­form­ing — it’ll be right there in your foot­er for you to eas­i­ly reference:

function footer_debug() {
	if (current_user_can('level_10')) {
		echo '<p>', get_num_queries(), ' queries. '; timer_stop(1); echo ' seconds.</p>';
	}
}
add_action('thesis_hook_footer', 'footer_debug', '99');

Once you’ve added the code, upload it to your site and vis­it your site to ensure every­thing is work­ing cor­rect­ly. View the site both while logged in and while logged out to make sure the debug infor­ma­tion only appears at the cor­rect time.

I hope that you’re able to use the debug infor­ma­tion to make your site even more lean & mean!


Posted

in

by

Tags:

Comments

7 responses to “Use WordPress Debug Stats to Trim the Fat from Your Blog”

  1. Mike Nichols Avatar

    Thanks, Rick, for this bit of code. It works like a champ and will allow me to see how my site is doing, and to debug if it’s too slow!

  2. Rick Beckman Avatar

    Thanks for the com­ment, Mike. I should’ve men­tioned in the post that one of the best ways I’ve come across to trim the query count while still using a vari­ety of plu­g­ins is to use WP Wid­get Cache plu­g­in.

    With the plu­g­in, you may have wid­gets which hit the data­base with a hand­ful of queries, but they’ll only be mak­ing those queries when­ev­er the cache is inval­i­dat­ed or cleared — far less often than on every page load!

    Obvi­ous­ly, cache plu­g­ins which cache the entire page as opposed to just the wid­gets would do even more to help, but frankly, those are a pain to work with, in my opin­ion. I tweak a lot, and the cache has to be cleared after every tweak to get it to work prop­er­ly; also, at least one plu­g­in I’m using (Pop­u­lar­i­ty Con­test) would­n’t do any­thing use­ful if it was cached.

    1. Ken Grogan Avatar
      Ken Grogan

      Rick, thanks for the tip on the WP Wid­get Cache plu­g­in. I like to use a bunch a wid­gets, so I tried it on my pages and it reduced my data­base queries from 75 per page down to only 20 per page. Very nice.

  3. Kelly the Kitchen Kop Avatar

    Rick, I checked the box, “add debug infor­ma­tion” on the open­hook page, but do I also need to add the code above? I don’t see any info in my footer.

    Thanks,
    Kelly

  4. Gregory Fisher Avatar

    Hey Kel­ly,

    I just test­ed the “add debug infor­ma­tion” in Open­hook and it worked per­fect­ly. How­ev­er, you have to keep the Word­Press back office open to see it. 

    Every page in the back office has your blog’s title in the upper mar­gin. If you go there and right click on the title, you can select “Open link in new win­dow” or if your brows­er is new­er, “Open link in new tab”. Either one will open the home page of your blog. Scroll down and you should see the infor­ma­tion at the very bot­tom of your foot­er, prob­a­bly in light gray font. I hope this helps.

  5. Luciano Bargmann Avatar

    Thanks for the tip Rick.

    I just want­ed to post a lil’ fix you need — and an advice for new programmers:
    Always use curly brack­ets on your if statements!

    In this case, if you are not logged in, you only see some­thing like 0.345 sec­onds with­out an open­ing p tag. This hap­pens because the semi-colon before timer_stop(1) clos­es the if, lead­ing to unde­sired behaviors.

    Here is the fixed version:


    function footer_debug() {
    if (current_user_can('level_10'))
    {
    echo ''.get_num_queries().' queries. '; timer_stop(1); echo ' seconds.';
    }
    }
    add_action('thesis_hook_after_footer', 'footer_debug', '99');

  6. Luciano Bargmann Avatar

    Sor­ry folks, some data got stripped out from my code above.

    Rick, can you edi your page to include the curly braces and cleanup my comments?

Join the Discussion

Your email address will not be published. Required fields are marked *

Use your Gravatar-enabled email address while commenting to automatically enhance your comment with some of Gravatar's open profile data.

Comments must be made in accordance with the comment policy. This site uses Akismet to reduce spam; learn how your comment data is processed.

You may use Markdown to format your comments; additionally, these HTML tags and attributes may be used: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Rick Beckman