Optimizing WordPress for Best Performance

by Karthik on August 13, 2007 · 21 comments

WordPress users have always had problems with their blog not loading when their blog posts have been dugg. Because of the very high traffic the blog goes down with MySQL errors, PHP errors etc.. But WordPress is a powerful blogging engine and you can take it to any levels you wish.. I am going to talk about how you can optimize your wordpress blog so that it can withstand any kind of load.. This topic has been widely debated in the past so i’ll just do a brief rundown to what has already been discussed and then pass over to tips that haven’t really been discussed so far.

  • Reduce DB Calls
  • Remove Plugins which are not in use
  • Use WP-Cache

While these tips are pretty much enough for everybody, but in case you want to extract that extra bit of juice from your wordpress blog then read on..


  • Compress your Stylesheet and Javascript

First make a copy of the both the Javascript and CSS files and save with an extension php. Once you have done that your header.php will look something like this.

Before
<link rel="stylesheet" type="text/css" media="screen" href="/style.css"/>
<script type="text/javascript" src="js/prototype.js"></script>

After
<link rel="stylesheet" type="text/css" media="screen" href="/style.css.php"/>
<script type="text/javascript" src="js/prototype.js.php"></script>

Once you have done that add the following to the very top of the style.css.php file

<?php ob_start ("ob_gzhandler"); header("Content-type: text/css; charset: UTF-8"); header("Cache-Control: must-revalidate"); $offset = 604800 ; $ExpStr = "Expires: " . gmdate("D, d M Y H:i:s", time() $offset) . " GMT"; header($ExpStr); ?>

and this to the very top prototype.js.php file

<?php ob_start ("ob_gzhandler"); header("Content-type: text/javascript; charset: UTF-8"); header("Cache-Control: must-revalidate"); $offset = 604800 ; $ExpStr = "Expires: " . gmdate("D, d M Y H:i:s", time() $offset) . " GMT"; header($ExpStr); ?>

Now that you have done all the hard work you should now know what does all this achieve. First of all the CSS and Javascript are compressed via gzip which is built into Apache. By using gzip compression you are saving tons of bandwidth and at the same time by enabling cache you are making sure that repeat visitors to your blog are not going to need the CSS and Javascript file once again. So effectively this means less HTTP requests and a huge saving of the precious bandwidth that you are paying for.

Advantages : Right now the Homepage of dailyApps is at 14KB and by enabling gzip it comes down to 4KB. The Main CSS file is 7KB and after gzip compression it comes down to 2KB. The Urchin Tracker JS file is 21KB and it comes down to 6KB. See the huge savings there.. gzip compression is a must have for anyone who wants to save bandwidth.

Disadvantages : At high server loads gzip compression can actually kill your server so you might want to disable gzip compression at high server loads.

  • Cache Images, Scripts Server side rather than depending on the Browser to do so..

Be very careful when proceeding with this. Make a backup of your existing .htaccess file before you continue with this step. By Caching Images, Javascript that don’t normally change over a period of time you can reduce HTTP requests and save bandwidth. To enable Caching add the following code to the end of your .htaccess file.

# Begin Cache Control
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType application/x-javascript A2592000
ExpiresByType text/css A2592000
ExpiresByType image/gif A604800
ExpiresByType image/png A604800
ExpiresByType image/jpeg A604800
</IfModule>

By Caching your images, Javascript you can see a hude increase in the load times of your site.. See this screenshot of this site for an example

dailyApps Cache Control

You can see that the number of requests as well as amount of data transfer has been reduced significantly which directly results in better performance of your wordpress blog.


  • Remove Etags

Etags are tags that is sent out by the Server to the browser to see if a file has changed. Etags can be quite buggy at times and it is best to simple HTTP caching for better performance. For more details on this see this page. To remove Etags just add the following code to the end of your .htaccess file.

#ETag Configuration
FileETag none

By doing this you are making sure that the Objects are cached properly and there is no ambiguity whatsoever regarding the request of the object.


  • Use Image Sprites

Image Sprites can be extremely helpful in reducing HTTP requests. For more details on how to use Image Sprites see this excellent howto written by Paul Stamatiou.



Well this is it.. Using these tips I am sure you can extract that extra bit of Juice from your WordPress installation. Remember most of the things that I have talked about here are related to HTTP requests and caching, so first time visitors will not see any kind of improved load times when they visit your blog. But after that, it improves the performance of the blog to some extraordinary levels..

Do comment and tell me how these tips have helped you to increase the performance of your WordPress blog.

P.S. Also note that these techniques will work pretty much well for any website or blog that runs on Apache.

See more from: Web

{ 6 comments… read them below or add one }

jameswillisisthebest September 9, 2007 at 3:58 am

This is my first post
just saying HI

Reply

Andrea Pernici December 3, 2009 at 3:57 pm

I tried to Compress Stylesheet and Javascript as you wrote here but the result is a page without css and js. Where is the problem ?

Reply

Slava December 15, 2009 at 11:48 pm

THANKS for post! I have finally understood why my blog does not compress css and js!

2Andrea: here is a little error, just add a plus (+) between “time() $offset” (before last string)
and you won't receive php errors

Here is what I use to leverage browser caching (in .htaccess):
<FilesMatch “.(ico|jpg|gif|css|png|js)$”>
Header set Cache-Control “max-age=29030400, public”
</FilesMatch>

Cheers

Reply

Maverick Money Makers Review June 14, 2010 at 2:58 pm

hi friend i look it and more time visit here!

Reply

Maverick Money Makers July 13, 2010 at 7:01 am

i think more people visit in this site!

Reply

pianoforall March 25, 2011 at 9:14 am

Really good advice. Great Article. Well spoken. Thanks.

Reply

Leave a Comment

{ 15 trackbacks }

Previous post:

Next post: