↑ Return to 2. Template optimization

2.7 Other Theme Optimisations


Page no:

 

Via Yoast

 

Every once in a while people will ask me to fix their blog, because it’s either slow, or broken. When it’s not something to do with their WordPress hosting, (some hosts are just plain bad and slow), most of the time this is caused by either broken plugins, or broken themes. There are a few things I tend to do when I get to clean up stuff, and I though I’d list them for you.

Clean up your theme

header.php
First of all, what I do is make the header.php file do a lot less queries. Because themes have to be easy to spread, they have to get almost all the blog specific info from the database. That results in a lot of queries for stuff that you could just hardcode into the theme. Some examples, taken from the default kubrick theme:

1 <html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes(); ?>>
2 <head profile="http://gmpg.org/xfn/11">
3 <meta http-equiv="Content-Type" content="
4   <?php bloginfo('html_type'); ?>;
5   charset=<?php bloginfo('charset'); ?>" />

Could just as well be:

1 <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr">
2 <head profile="http://gmpg.org/xfn/11">
3 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

You can also:

  • make your stylesheet URL’s static
  • make your pingback URL static
  • make your feed URL’s static
  • you can remove the blog’s WordPress version
  • make your blog’s name and tagline / description static

Doing all that, you can remove 11 queries to the database, and this can highly speed up your theme.

footer.php
The default theme also has some of these calls to the database in the footer which you can make static, or remove altogether:

  • your blog’s name
  • RSS feed URL
  • Comment RSS feed URL

You can also remove all comments that aren’t necessary, like “If you’d like to support WordPress, having the “powered by” link somewhere on your blog is the best way; it’s our only promotion or advertising.” I can tell you that that line is in a LOT of footers, and it’s a waste of bandwidth once you have decided to leave the link for WordPress in or not.

Check your coding habits

You will have added code to your themes for your plugins. Let’s say you have a line of code like the one below, for a plugin that thanks people coming from search engines:

1 <?php refer_thanks(); ?>

This creates a problem, as soon as you, by accident or another cause, disable the plugin that holds the refer_thanks function. When the function doesn’t exist, the code errors out, and your page doesn’t continue to load, thereby breaking your blog. To fix this, PHP offers a special function called function_exists, and using it, the code would look like this:

1 <?php if (function_exists('refer_thanks')) { refer_thanks(); } ?>

Now, if the function doesn’t exist, your theme, and thus your blog, won’t break. It’s probably a good idea to do this for every line of code you added for a plugin.

 



Realisation on snbchf.com

It is good to clean up our code of not necessary parts of it.

Realisation on newsfeed site

See more for SEO