Speed up WordPress, and clean it up too!

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:

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:

<?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:

<?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.

Post author: Joost de Valk

Joost de Valk is the owner and creator of Yoast.com. He's a WordPress / Web developer, SEO & and an Open Source fanatic.
He's also (and more importantly) the father of three sons called Tycho, Ravi and Borre, a daughter called Wende and the husband of the lovely Marieke, who also works at Yoast. Read all about Joost »

If you are using caching, is it still necessary to remove all the queries from the header and footer? It seems like they’d be queried once to generate the cached version of the page and then the cache would hit after that.

Be careful with the cache option. Some results will change when implemented. For example: Most Popular will no longer reflect accurate results. The pages are now cached and therefore will not update the database with each hit.

Joost, about the theme and UTF-8. With this encoding, the characters won’t be showed correctly. If you’re using < MySQl 5, MySQL uses default ISO-8859(-1 ?). The solution is the use of extra queries to change the character encoding.

Thanks for the tips Joost. I used to use wp-cache, but found my self always turning it off and then on again durring development. Now I have put in a development server this shouldn’t be a problem and I can confirm that when running it really did make the site faster. I like your other tips to reduce database access, but with cp-cache, is this not elliminated anyhow?

I don’t remember the specific post, but it was only like a week ago that Matt Cutts suggested removing the WordPress Version tag to cut down on hacking attacks so that’s definitely an important one to remember. If hackers can easily find out what version of WordPress your running, especially if it’s an older version, then it makes hacking all that much easier.

Yeah, thanks for the tips, my blog changes themes often to. Maybe a few tweaks will get it to work. As a web designer I have found a lot of problems with wordpress and speed is one of the main issues in complicated sites. Thanks.

Sorry if this is off-topic, but since we’re talking about optimization here such as caching, it sparks a recent question that I have.

I was reading about your Google Analytics plugin and it seemed that one of the selling points was that it was running mostly on the server-side. I don’t question the benefits, but I was wondering what happens when you run it with a caching plugin such as WP-Super-Cache. Is it safe to run your analytics plugin with super cache or does the cache interfere with it?

Great, simple tips. I just went through and changed the header files of my blogs to use the static info, and it really did speed up the loading time significantly. As always, you are a font of great info. Thanks.

good tips, as long you don’t want to migrate your modified theme to another site…

A simple way to hardcode is to check the source-code of your site and copy and paste the entries in header and footer. Like this you take whatever charset your blog suggested with the language attributes requested and the correct path to the stylesheet etc.
(don’t do this thing with index.php as that should really be generated new…)
thanks, Joern

Very comprehensive, thank-you. One point of concern for me: I have never seen any benefits from any type of cache plugin. In every case my sites are always slow to load the first time and only then, do pages load faster. This is no good for first time visitors and only serves regulars. Is this a typical cache behaviour? if not what could I be doing that’s wrong. To my mind, the first hit must be the fastest as this is the visit that gets you hooked or gets aborted for slow loads.

Nice tips. You can speed up WordPress even more if you turn on MySQL and PHP caching. I made same benchmarking on fresh WordPress installation (no modules and empty database) and the gain was about 150%. I used ab (Apache benchmarking tool) and document all on my site. Next step will be to install WordPress caching module and make WordPress fly :)

Can you provide your goods and services into the educational sector in your area. if you can and you are willing to offer the schools a discount, you could find your business in front of all the educational establishments in the uk. you also get direct communication with headteachers to update them with your special offers etc

I had a good laugh the other day. I work between an online blog and a test bed I use for blog development.

I recently downloaded several files from my online blog to the test bed I use for blog development. I did this in order to insure that the files, including style.css, were identical at both locations.

However, I had forgotten that I had made the links in the online header.php file static.

I did not notice this for a frustrating day or two. I tried to make several styling changes in my local copy of style.cssâ€”but the changes never made it to my browser’s display!

Eventually, I noticed that the browser was not looking at the style.css file on my test bed but was always retrieving the stylesheet from my online blog. Once I discovered this and corrected the header.php file to show the right link to my local stylesheet, the changes began to alter the HTML display as expected.

I took a look at my blogs database and noticed that wordpress creates “revision” rows for every post every time I edit a post. I think that removing this “revision” rows will also help, specially blogs with 1000’s of posts, speed up queries since there will be less records in which to look for.

By Shack Dougallon 4 April, 2009

@MiguelAvarez – The Revision Control Plugin can really help if you want to limit or disable the number of revisions.

Hey thanks alot for the write up. I have been searching for awhile for this information and have been unable to find a write up this comprehensive. I have alot of work to do on my older sites with the clean up and have experienced alot of errors with it but hopefully this helps. thanks again

Thanks, oh my god that was so needed. A thing to clean all my dirty plugins. Sweet!

By Michaelon 16 June, 2009

I currently have a blog with 6,290 pages. Once I created this many pages it slowed down so much that it about to hang up the idea of using wordpress to manage my site. Any suggestions to speed this aside from your mentionings above?

By Richardon 9 July, 2009

wordpress by itself scales fairly well. The real problem comes when install plugins that add queries to a post before the page is rendered. Typical plugins are related post plugins. The more posts you have the more CPU work is required building a related posts list before the page is rendered. If you are using any plugin that needs to query all your posts before spitting out content, you should consider removing these, or using an alternative that caches results. YARPP does use cacheing for its related posts, so this may be a useful alternative.

There are many other things to look at both in design and server side … really beyond the scope of a short comment.

Okay, this may be one of those its late & im not thinking clearly kinda questions but why would you not add lang=”en” (or whatever language is appropriate) to the html tag? Before you “cleaned it up” it declared the language but once it was “cleaned” it didn’t or am i missing something? Just curious! Great post!! :D

[…] WordPress template optimization – WordPress – Joost de Valk’s SEO Blog Every once in a while people will ask me to fix their blog, because it’s either slow, or broken. 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 (tags: blog blogging Code Hacks Optimization wordpress) […]

[…] Optimize your theme’s template by getting rid of unnecessary PHP tags because some of them just lead to unnecessary queries on your server. The following tags which can be found in your theme’s header.php, sidebar.php or footer.php can be hardcoded into the template thereby reducing server load. Here’s a simple way of doing this. Open your blog’s home page in Firefox. When it’s done loading, right-click on it and select view page source. This immediately pops up a page that shows you the html of your blog’s home page. Simply open your theme editor and start replacing the php tags above with the urls that have taken their place in the page source view. For more information check out Problog Design’s posts 13 Tags To Delete From Your WordPress Theme & 10 Ways To Speed Up Your WordPress Blog and also Yoast’s Speed Up WordPress. […]