Monday, July 9, 2012

Speed up WordPress, and clean it up too!

- no title specified

Speed up WordPress, and clean it up too!

by Joost de Valk on 21 January, 2008 at 15:17 110 comments

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:

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:

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 waist 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 therefer_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 calledfunction_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.

Install a caching plugin

Lastly, you should really consider installing either WP-Cache, or WP-Super-Cache. They make your blog so much faster, that you won't know what happened to you once you've got them running.

All of this makes sure your blog remains maintainable, and fast.


No comments:

Post a Comment