From PHP to HTML

PHP is great, but there is one thing about it.   It has to be loaded with a call to the database to grab the data to display.   Each of those “calls”, a database query takes little bit of time to execute.   Therefore, if you can save a few of those trips to the database you have increased your load time.

Theme Design is Generic

Theme designers use PHP calls to fill in things like the Site name, description, etc.   The data they pull is usually in the header, footer, and sometimes in the sidebar.

But now that we have the theme chosen for our site that data isn’t going to change.   We can use this to speed up the load time of our site.

Before we go making changes to our theme header.php and footer.php we should make a backup of our theme files.

Now that the backups are made we can decrease the database queries and hopefully gain a bit of speed on our load times.

For a benchmark if you have already installed the code to your theme that tells you your load time and queries executed then write that down.   After you are done check to see how many queries to the database were saved.

If you aren’t familiar with what I’m talking about here is the code.

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

Paste this in your footer.php just before the close body tag “</body>”

Replacing PHP with HTML

Here are the step we will take and I’ll use one of my photo blogs as an example.     We should see   a decrease in the number of database queries.   For this example we will only be modifying the header.php file.

  1. Open the header.php file
  2. Find php code calls to stylesheets, blog title etc.
  3. View the blog in a browser tab
  4. Right click and “Veiw Page Source”
  5. Find the HTML code that the PHP generates
  6. Copy the code
  7. Paste the HTML into the header.php where the PHP code is currently.
  8. Save the header.php
  9. View the blog after making the change to verify it is all there.
  10. Right click and “View Page Source” and compare to the one you copied from
  11. Check to see the page load time and number of queries executed.

Header PHP code Replacement

Header code:

<link rel=”stylesheet” type=”text/css” media=”all” href=”<?php bloginfo(‘template_directory’); ?>/css/960.css” />
<link rel=”stylesheet” type=”text/css” href=”<?php bloginfo(‘stylesheet_url’); ?>” media=”screen” />
<link rel=”alternate” type=”application/rss+xml” title=”RSS 2.0″ href=”<?php if ( get_option(‘woo_feedburner_url’) <> “” ) { echo get_option(‘woo_feedburner_url’); } else { echo get_bloginfo_rss(‘rss2_url’); } ?>” />
<link rel=”pingback” href=”<?php bloginfo(‘pingback_url’); ?>” />

Replace with:

<link rel=”stylesheet” type=”text/css” media=”all” href=”http://www.paintingtucson.com/. . ./css/960.css” />
<link rel=”stylesheet” type=”text/css” href=”http://www.paintingtucson.com/wp-content. . ./style.css” media=”screen” />
<link rel=”alternate” type=”application/rss+xml” title=”RSS 2.0″ href=”http://www.paintingtucson.com/feed/” />
<link rel=”pingback” href=”http://www.paintingtucson.com/xmlrpc.php” />

Header H1 Code:

<h1><?php bloginfo(‘title’); ?></h1>
<h2><?php bloginfo(‘description’); ?></h2>

Replace with:

			<h1>Painting Tucson</h1>
			<h2>With Light</h2>

Is it worth it? In most instances I don’t think it is. In our example, by making these changes I saved 3 queries. It went from 116 queries to 113 loading the home page.

So why bother?   For most of my smaller blogs I don’t.   But for the Tucson blog where I have over 800 posts and about 100 pages it saved a lot.   For one reason, I have a lot of custom community pages on that blog.   Those links were all listed in the sidebar.   Those pages never change.   By taking those sidebar links and turning them into straight HTML I saved close to 70 database queries on every page load.

Before starting this project that blog would take about 7 seconds to load and 9 seconds wasn’t uncommon on some of the pages with more than one image.   Now, with gzip and a few of these kinds of tweaks to the site I’m seeing some 1.9 second page load times. That is a significant difference.

You be the judge.   If you don’t need to do this, don’t bother.   (If you change themes, you have to do it all over again).   But if you have a lot of database queries and your page load times are lagging then squeezing every millisecond out of your load time is worth it.

Comments

  1. Great information for my real estate site…I have been thinking about this a lot, but I wasn’t sure about what/if/how to change. My homepage loads in 2.25 seconds and last time I check I had like 130’is queries. I would love to speed things up, but I am running some heavy plugins.

    What about cleaning up XHTML errors, is it worth it? Right now I have 94, but about 60 of them are coming from 4 plugins from Altos Research? I need to start looking into this stuff and optimizing the speed of my site. Thanks for sharing….and making me think about it :).
    .-= DJ Morris´s last blog ..Abacoa Real Estate Scams in Jupiter, Florida =-.

  2. DJ,

    It is unfortunate that often it is the RE plugins that do the worst job of XHTML compliance. The good news. If you know any code you can often weak the code to bring it into compliance. The other piece of good news in this: One error in the code can often result in multiple errors being reported. I’ve fixed an error in the code before and found that one fix actually removed about 25 of the errors reported. When you see the same line of code referenced over and over again in the error report this tends to be the case.

    The other side, when they come out with an update to the plugin they often haven’t fixed the code. The motto seems to be: “If it works, it works. Why do I care of your site is compliant or not.”

    Dropping an note to Altos and pointing out the errors being generated is a good first step in helping a lot of users, and ultimately Altos itself.

    Dave

  3. I actually was thinking of hiring one of the techies over at the O-desk to clean things up for me…I tried to do a little of it, but I couldn’t figure out where to find the lines the errors were on, but I’m not a techie. I did a lot of the changes to my css myself using W3 Schools, but php scares me….what is the best beginner guide to php? I was actually thinking of getting PHP for Dummies LOL….but I’m being serious :)?

  4. The errors you are getting for XHTML non compliance won’t be generated by php code.

    The php I do know I have picked up on my own from studying code itself and researching when I needed a specific function. Usually WP related.

    Using the WC 3 error check site and looking at the code has been my tool for reducing the number of errors. I tired for years to run completely error free. I had to decide if some plugins were worth a couple of errors as time went on. I’ve chosen to live with a few errors. But I try and keep it to under double digits. Which reminds me I haven’t checked my sites in a while. Hum, ignorance is bliss?

    I would try a few of the things in this series of posts on decreasing your page load time and live with it. I’ll try and get some time to check your site later today and see what I find.

    Dave

  5. DJ,

    Okay, I cheated, I ran your site url through the validator. 32 errors, 2 warnings, a lot of them are javascript style issues, not a biggie for speed. A few were open ended coding issues like no close or a break code that isn’t self closing instead of the valid code.

    Here is the link to the results:

    http://validator.w3.org/check?uri=http%3A%2F%2Fjupiterflhomes.com%2F&charset=%28detect+automatically%29&doctype=Inline&ss=1&group=0&user-agent=W3C_Validator%2F1.767

    I wouldn’t hire anyone to fix this stuff. The simple ones you can probably find and fix. The javascript . . .

    Dave

  6. “I would try a few of the things in this series of posts on decreasing your page load time and live with it. I’ll try and get some time to check your site later today and see what I find.”

    That would be awesome, and much appreciated! I tried my best and did most of the site myself on a redesign of the Agent Press Theme…it’s only been live since January and I just have so much to learn…ie when to use a tag and when to make a new category :)…I hate to admit my ignorance, but you have to start somewhere. Probably a stupid question, but what does that red down arrow mean on my last blog post link at the bottom?
    .-= DJ Morris´s last blog ..Abacoa Real Estate Scams in Jupiter, Florida =-.

  7. DJ,

    Are you referring to the heart bubble? When you mouse over it you will see the explanation. It means you get a do follow back link to your last blog post. I use a do follow plugin that after three comments the last blog post is displayed and a do follow link provided to your post.

    Link Love (the reason for the heart in the bubble)

    Dave

  8. Not the heart…the heart pops up after I post the comment, but while I’m writing the comment there is a red down arrow after the link to my last blog post….then the down arrow becomes a heart after I submit the comment?

    You inspired to sign up for comment luv, but they gave me a blog…I have too go back and grab the plugin file.
    .-= DJ Morris´s last blog ..Abacoa Real Estate Scams in Jupiter, Florida =-.

  9. DJ,

    Since I”m the admin I’ve never seen it. So the best answer I can give is “I have no idea” probably related to that plugin. But that is just a guess.

    Dave

  10. Sorry for hijacking your thread :). Real dumb question, but are these xhml errors fixed through the dashboard of wordpress or in the actual edit of the page or post, and how do you know where the line they are referring to are?

    I am going to owe you big time for your time, but I’m new at all this….selling houses is easy compared to this stuff :)!
    .-= DJ Morris´s last blog ..Abacoa Real Estate Scams in Jupiter, Florida =-.

  11. DJ,

    They are in both places. Some are in posts, others in the theme code. You have to look at the context as well are the line reference. The easiest was is right click on your home page and “View Source” or View Page Source depending on your browser. Then search for the phrase that is in the line error. (open two tabs. One on the validator page and one on the blog, even a third on the admin theme editor) then you can find and begin correcting errors..

    One thing you can do easily to eliminate a couple is to change one line in your header.php file.

    from this:
    < !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"to this:

    < !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

    It isn’t as “Strict” in enforcement of validation.

    For those errors in posts like “target= ” you can go into the post itself and remove the target statement or fill it out completely. But those are minor. errors. Something you can deal with when writing posts an inserting links.

    Hope this helps rather than muddy the water.

    Dave

  12. You can also use the Super Cache plugin. Probably a better option for people that aren’t comfortable editing their themes.

  13. I agree with Dan. It was much easier for me to just use the wp super cache plugin. Great post though, in the future i’ll use your method. =]

  14. Troy,

    I don’t use a cache program because I’m constantly making changes, tweaking the site. And as a designer it isn’t practical for me. If a site is pretty static in nature I can see it as a quick way to increase your load time.

    Dave

  15. I never thought about replacing PHP with HTML just to speed up page generation. Greta tip! Thanks!

  16. This is an approach I never considered. I’ve have been using caching to speed things up, but this has the potential to work as well or better in come cases.