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:

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

Could just as well be:

<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr">
<head profile="http://gmpg.org/xfn/11">
<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:

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

Optimize your tags

I created a tool to optimize your tags easily, read more about it here.

Install a caching plugin

Lastly, you should really consider installing either W3 Total 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.

Tags: ,


Yoast.com runs on the Genesis Framework

Genesis theme frameworkThe Genesis Framework empowers you to quickly and easily build incredible websites with WordPress. Whether you're a novice or advanced developer, Genesis provides you with the secure and search-engine-optimized foundation that takes WordPress to places you never thought it could go.

Read our Genesis review or get Genesis now!

110 Responses

  1. Gyutae ParkBy Gyutae Park on 21 January, 2008

    These are great tips Joost. Especially important for preparing for any traffic spikes as well.

  2. Adam DempseyBy Adam Dempsey on 21 January, 2008

    Some great tips! never thought of hard coding the header values, going to try that now, thanks :)

  3. David BradleyBy David Bradley on 21 January, 2008

    Excellent tips. I’d covered most of those but had overlooked the meta “text/html; charset=UTF-8″ reference. Fixed that now and blogs are loading 2 ns faster ;-)

    db

  4. Luke HoerstenBy Luke Hoersten on 21 January, 2008

    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.

  5. Joost de ValkBy Joost de Valk on 21 January, 2008

    @Gyutae Park: yup, very true.

    @Adam Dempsey: yeah it’s weird, hardly anybody seems to do that.

    @David: hey, it’s the small things ;)

    @Luke: well, every comment invalidates (iow clears) your cache, so I’d do both.

  6. Henri van den HoofBy Henri van den Hoof on 22 January, 2008

    Excellent suggestions :-) I have used it to adapt my blog’s theme and even some of the plugins too. Every small improvement of loading time helps.

  7. ScooterBy Scooter on 22 January, 2008

    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.

  8. RockyBy Rocky on 22 January, 2008

    Good post. Speeding up improves user experience, which is important.

  9. HendrikBy Hendrik on 22 January, 2008

    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.

  10. Joost de ValkBy Joost de Valk on 22 January, 2008

    Hmm Hendrik, that’s a nice point… In most cases, WordPress set’s it to UTF-8. Are you suggesting that setting either MySQL to UTF-8 or the blog to ISO-8859 would speed things up?

    And btw, ISO-8859 and ISO-8859-1 don’t support the € character… So it should be ISO-8850-15 ;)

  11. Joost de ValkBy Joost de Valk on 22 January, 2008

    @Scooter: the question is: do you care about the popular posts being completely correct all the time?

  12. HendrikBy Hendrik on 22 January, 2008

    Joost, you’re wrong ;)

    UTF-8 doesn’t support €. ISO-8859 en ISO 8859-1 work fine.

    My exprience with UTF-8 is wrong:

    - MySQL
    - .htaccess
    - PHP headers
    - Meta characterset

    After converting to UTF-8, symbols as € (will be a square), ë, é, etc. This characters will be convert to strange characters. Currently, I use ISO-8859-1. That works fine for European content.

  13. Allan StewartBy Allan Stewart on 22 January, 2008

    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?

    Cheers,

    Allan

  14. Joost de ValkBy Joost de Valk on 22 January, 2008

    @Hendrik: I’ll dive into it tonight, you’re effectively saying that putting it into ISO-8859-1 is easier and faster, sounds good ;)

    @Allan: it does when you’re serving a cached page, however, each comment invalidates that cached page… I’d do both.

  15. KyleBy Kyle on 23 January, 2008

    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.

  16. MatthiasBy Matthias on 23 January, 2008

    Thanks for the tipps.. You are right, with some work it might be possible to speed up wordpress, and with every new plugin mine gets slower and slower..

    i will really need to check my code if i find time..

  17. Ruud KokBy Ruud Kok on 23 January, 2008

    Tnx for the tips Joost, hope this will stop my blog from changing themes all the time.

    By the way, the post Kyle is refering to is this one: http://www.mattcutts.com/blog/three-tips-to-protect-your-wordpress-installation/

  18. Joost de ValkBy Joost de Valk on 23 January, 2008

    @Kyle & Ruud: read it too, read Matt’s blog daily, of course ;)

  19. New Orleans SEOBy New Orleans SEO on 24 January, 2008

    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.

  20. AllenBy Allen on 24 January, 2008

    These are some great tips. I’ll have to look into implementing some of them, especially when I start generating more traffic.

  21. StuffelBy Stuffel on 25 January, 2008

    Hi Joost, nice post, thank you. In my blog, the UTF-8 works great, the same with the Euro sign. can’t see any problems.

  22. mediamindBy mediamind on 25 January, 2008

    Hi,
    i think you should also take care with commands, tag cloud and similar posts. tha last one´s are plugins that use many requests.

  23. Shack DougallBy Shack Dougall on 26 January, 2008

    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?

  24. Patrick DalyBy Patrick Daly on 29 January, 2008

    The reduction of queries makes total sense – thanks for even more tips!

  25. Lars BachmannBy Lars Bachmann on 29 January, 2008

    Thank you. Just what i needed. A guide to clean up my WordPress blog. I have lots of old plugins and themes, maybe it’s time to delete some of them.

  26. MarioBy Mario on 6 February, 2008

    Thanks much to do…great feed

  27. DazzlinDonnaBy DazzlinDonna on 10 February, 2008

    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.

  28. JoernBy Joern on 18 February, 2008

    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

  29. Marketing ArticlesBy Marketing Articles on 14 April, 2008

    I’m googling on how to fix a broken theme and I found this site. Thanks for the tip although I did not get it the first but after several tries it’s already done.

    -Jan

  30. Jacob StoopsBy Jacob Stoops on 7 May, 2008

    Great article. I had a lot of jargon lounging around my wordpress blog…old themes and plugins.

    This really helped me clean it up :) Thank you so much.

  31. Marios AlexandrouBy Marios Alexandrou on 11 June, 2008

    Where’s the plugin that will transform every post into quality linkbait and submit to all of the top tier social networks? :-)

  32. cesarBy cesar on 13 June, 2008

    Hello, i can´t see the lines of code that haev to be change on the headerç

    PD: sorry for my bad english, i’m from Argentina

  33. SudarBy Sudar on 1 July, 2008

    I guess there is some problem with your code tag. Some of the code in the article are not displaying properly.

  34. Joost de ValkBy Joost de Valk on 1 July, 2008

    Thx for noticing Sudar, fixed.

  35. JPBy JP on 24 July, 2008

    My question is, how do you tell what each php code represents, so you can replace it with hard coded values?

    For example, is replaced with “UTF-8″

    How do you check what charset is being used to know how to replace the php tag with a static one?

    There are others as well, for example:

    =”

    etc.

    Thanks!

  36. JPBy JP on 24 July, 2008

    Sorry folks I figured it out. I just loaded the page and viewed the page source to see what the code was being converted to. Then I just pasted that into the header.php.

    Thanks!
    –JP

  37. RichardBy Richard on 31 July, 2008

    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.

  38. Make it faster | www.redips.netBy Make it faster | www.redips.net on 27 August, 2008

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

  39. BeijingBy Beijing on 31 August, 2008

    I have changed all the header links to static after reading this article, and installed wp-cache, now the site is loading much much faster.
    Thanks for the sharing.

  40. Phoenix Window Cleaner GuyBy Phoenix Window Cleaner Guy on 3 September, 2008

    @Sudar:

    It usually is just something simple. Take a moment to double check it all then try it again.

    It really doesn’t help it load much faster and is well worth the time spent.

  41. HolgerBy Holger on 24 September, 2008

    a tip from http://www.dailyblogtips.com/remove-the-hyperlink-from-single-post-titles/
    some more tuning tips inside the blog – great informations

  42. HandandPawBy HandandPaw on 13 October, 2008

    Your tips are great! Thank you so much for taking the time to post them and help people such as myself clean up our blogs, make them faster nad user friendly!

    Cheers!

    Jess

  43. Tony ThompsonBy Tony Thompson on 14 October, 2008

    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

  44. RajBy Raj on 15 October, 2008

    Nice tips. I am using Wp_Cache since a long time and it works!

  45. Royce TivelBy Royce Tivel on 16 November, 2008

    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.

  46. LitsoBy Litso on 21 November, 2008

    Spam ^

    Anyway, I needed to leave a reply somewhere to see your relish plugin at work. Ignore this message.

  47. raj 3gpindiaBy raj 3gpindia on 27 March, 2009

    Very nice information.i am looking for this for long time.i bookmarked your blog ,Thanks for this.

  48. MiguelAlvarezBy MiguelAlvarez on 4 April, 2009

    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.

  49. Shack DougallBy Shack Dougall on 4 April, 2009

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

  50. JoshBy Josh on 7 April, 2009

    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

  51. FoeLBy FoeL on 20 April, 2009

    Useful tips for speed up wordpress blog, i use WP-Super-Cache as my chace plugin, i installed this plugin i see my blog seems more even faster than before…
    Thanks for the tips

  52. Matt@ThisBarSucksBy Matt@ThisBarSucks on 29 April, 2009

    Some great tips. I have also gone to http://validator.w3.org/ to see what can be fixed. Looks like I am finally going to have to learn code :-)

  53. YouriBy Youri on 6 May, 2009

    I prefer to set the meta tags static (maby a little bit work, but hey who cares!).

    Thanks for the post!

    – Youri ( devfolio )

  54. helsekostenBy helsekosten on 12 June, 2009

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

  55. MichaelBy Michael on 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?

  56. RichardBy Richard on 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.

  57. seo companyBy seo company on 28 July, 2009

    Thanks for this.

  58. joyoge designers' bookmarkBy joyoge designers' bookmark on 10 August, 2009

    very helpful informations for seed up wp, thanks a lot..

  59. fusharBy fushar on 23 August, 2009

    Nice article! I’ve implemented your tips on header.php, hoping for some speedup :)

  60. Mr.LaptopBy Mr.Laptop on 23 August, 2009

    Thank you very much for your good sharing.

  61. Dani McDanielBy Dani McDaniel on 26 August, 2009

    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

  62. sunnybearBy sunnybear on 17 September, 2009

    There is a special plugin for WordPress – Web Optimizer, http://wordpress.org/extend/plugins/web-optimizer/ – that can increase client side load speed oy your weblog on WordPress by 200-500% in one click.

  63. reportthewebBy reporttheweb on 6 October, 2009

    Excellent tipps in this case… but sunnybear whate exactly is this script ?

  64. s.holstensBy s.holstens on 8 October, 2009

    Thank you for the Tipps =)

  65. Andy FitzpatrickBy Andy Fitzpatrick on 20 October, 2009

    Cheers for the tip about Web Optimser I’ve been looking for a solution to this all day.

  66. Web Penghasil DuitBy Web Penghasil Duit on 21 October, 2009

    Thanks for the tips. I like it!

    Web Penghasil Duit

  67. arianitBy arianit on 7 November, 2009

    Excellent tips, I used it :) Thanks

  68. Watch Movies Online FreeBy Watch Movies Online Free on 24 November, 2009

    Excelent Tips man, thanks

  69. w3digg.comBy w3digg.com on 26 November, 2009

    Excellent tips… is there any suggestion about html compression.

  70. Mr WeddingtonBy Mr Weddington on 2 December, 2009

    Brillaint tips this was really really helpful

    thanks

    Matt

Trackbacks

  1. [...] 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) [...]

  2. [...] 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. [...]