WordPress Speed and Optimization Guide
The WordPress self-hosted blogging platform is notorious for eating up server resources.
The vast majority of websites and blogs around the world that use WordPress, are doing so on a shared server. A shared web-hosting service means that many websites, reside on one web server, connected to the internet.
All of the websites on a shared host are utilizing and competing for the same physical resources, such as CPU, hard-disk space, RAM, bandwidth, etc.
Because of this, web hosts allocate a certain portion of server resources to each website. The higher the hosting plan (i.e. the more you pay), the more resources you are allocated.
If you cross this threshold, your web host will most likely throttle (or choke) your allowance.
If your website or blog continually uses more than you are allowed, you site will be suspended until you can resolve the issue.
Often, newbie bloggers are unaware of this.. until they receive a warning from their web host.
Final Notice – High CPU Usage
It wasn’t until I received a final notice from my web host regarding my blog’s CPU usage, which had been around 100% for a little while, that I decided to really investigate ways to bring this down.
This is the notice I received,
Dear valued customer,
Final Notice – High CPU Usage
Recent statistics has revealed that over the last few weeks your hosting account is exceeding the acceptable CPU usage allowance that is allocated to your hosting plan. You would have also received prior notification from our system regarding this.
This excessive usage could be caused by a number of factors but is most likely due to an incorrect setup of a 3rd party script or by having too many features, modules or plug-ins enabled on your web site.
What to do?
You can view your CPU usage history through the cPanel interface in the logs section under the /usage_logs/ directory.
If you simply have a very busy or popular web site, you may need to upgrade your web site to a new account with a higher CPU allocation.
Your website cannot continue to stay on the current plan at the current CPU usage. We strongly suggest that you contact our team as soon as possible to discuss your options in detail. In most cases this issue can be resolved by a simple upgrade to a higher-level plan. The process takes only a few minutes.
If no contact is made, or a resolution cannot be agreed upon by the 11rd March 2010, we reserve the right to suspend your account as per the hosting Terms and Conditions.
This post outlines some methods I have employed to help,
- reduce WordPress CPU usage
- speed up WordPress
- optimize WordPress
These methods and recommendations are outlined in no particular order..
Baseline
It is a good idea to measure the speed of your website and page load time before optimizing it.
That way you have got a tangible baseline from which to work off.
Here are a few sites that can be used to measure the speed of your pre-optimized site,
Update WordPress
Ensuring that your blog is running on the latest version of WordPress highly recommended.
With each new release comes bug and security fixes, increased stability and performance improvements.
If you blog is running slow, and you aren’t on the latest version.. I would suggest you upgrade.
Update Your Theme
Any quality premium WordPress theme will also be regularly updated to address known defects.
Updating your theme to the latest version.. can help with any performance issues.
I have written a guide on how to upgrade your WordPress theme to a newer version here.
Plug-ins
Delete
Often plug-ins are poorly coded and can be big offenders when it comes to resource usage.
Plug-in conflicts are not uncommon and can cause your CPU usage to go through the roof.
Have a think about which plug-ins you don’t really need and delete all non-essential plug-ins.
Deactivate
After deleting all unnecessary plug-ins you will still have your most essential ones remaining.
Are all of these used on a daily basis?
Can any of these plug-ins be deactivated and left dormant until they are needed?
For example, I have a plug-in called Maintenance Mode which adds a splash page to my blog that lets visitors know it is down for maintenance.
It doesn’t need to be live all the time, and can be left deactivated until needed,
Disable Logging
If a plug-in has the option to log events or results.. choose to have this option disabled.
Continual writing to the database requires CPU usage, which can result higher than normal utilization.
An example of this is the spam blocking plug-in Bad Behavior.
By default this plug-in has logging enabled.. which I found really increased my CPU usage.
By disabling logging, my CPU usage also decreased,
Caching
In my opinion, caching should be integrated into the core WordPress code, as a standard feature. It probably will become a core feature at some point..
A caching plug-in is a must have for any blogger who uses WordPress.
Caching plug-ins work by caching WordPress pages and storing them in a static file.
It is these static files that are served to your blog’s visitors.. rather than loading and compiling the whole PHP code and the building the page from the database.
Cached pages have a configurable expiry time, allowing for pages to be refreshed periodically.
W3 Total Cache
For the first year of my blog’s life I used WP Super Cache and during this time I was quite happy with it.
However, I was hearing some really good reports about another caching plug-in called W3 Total Cache.
W3 Total Cache is used and trusted by many popular blogs, including Mashable.com.. so I thought I would give it a go.
Some benefits that W3 Total Cache provides are (quoted from their site):
- At least 10x improvement in site performance (when fully configured: Grade A in Y Slow or great Google Page Speed Improvements)
- “Instant” second page views (browser caching after first page view)
- Reduced page load time: increased visitor time on site (visitors view more pages)
- Optimized progressive render (pages appear to load instantly)
- Improved web server performance (easily sustain high traffic spikes)
- Up to 80% Bandwidth savings via Minify and HTTP compression of HTML, CSS, JavaScript and RSS feeds
Features:
- Compatible with shared hosting, virtual private servers and dedicated servers / clusters
- Transparent content delivery network (CDN) integration with Media Library, theme files and WordPress itself
- Caching of (minified and compressed) pages and posts in memory or on disk
- Caching of (minified and compressed) CSS and JavaScript in memory, on disk or on CDN
- Caching of RSS (comments, page and site) feeds in memory or on disk
- Caching of search results pages (i.e. URIs with query string variables) in memory or on disk
- Caching of database objects in memory
- Magnification of posts and pages and RSS feeds
- Magnification (combine and remove comments / white space) of inline, embedded or 3rd party JavaScript (with automated updates)
- Magnification (combine and remove comments / white space) of inline, embedded or 3rd party CSS (with automated updates)
- Browser caching of CSS, JavaScript and HTML using future expire headers and entity tags (E Tag)
- JavaScript grouping by template (home page, post page etc.) with embed location management
- Non-blocking JavaScript embedding
- Import post attachments directly into the Media Library (and CDN)
After setting this up on this blog the speed gains that I noticed, compared to WP Super Cache were immediate and dramatic!
Recommended Reading:
GZip
GZip compression is when your webserver compresses your web pages, then sends these zipped files across the internet to whoever is viewing your website.
This person’s browser then automatically uncompressed these files.
Smaller files = faster downloads = happy users!
I have previously written about How to Speed Up your Website with GZip Compression, which shows how you can enable this on your WordPress blog.
W3 Total Cache also provides GZip functionality.
Hard-coding Static HTML Files
It is also possible to save CPU processing time and database queries by hard-coding static html, within some of your theme’s files.
So the below PHP code can effectively be replaced by hard-coding an actual URL.
Make sure your backup before doing this..
<?php bloginfo('rss2_url'); ?>
- can be replaced by – http://feeds2.feedburner.com/yourfeedburnerID
<?php bloginfo('stylesheet_directory'); ?>
- can be replaced by – http://example.com/wp-content/themes/yourtheme
<?php bloginfo('template_directory'); ?>
- can be replaced by – http://example.com/wp-content/themes/yourtheme
<?php bloginfo('template_url'); ?>
- can be replaced by – http://example.com/wp-content/themes/yourtheme
<?php bloginfo('name'); ?>
- can be replaced by – your blog’s title (eg “The Social Media Guide”)
<?php bloginfo('pingback_url'); ?>
- can be replaced by – http://example.com/xmlrpc.php
<?php bloginfo('stylesheet_url'); ?>
- can be replaced by – http://example.com/wp-content/themes/yourtheme/style.css
Recommended Reading:
Offloading Files
Registered members of this site have access to download and share most of my posts as PDF files.
Originally these PDFs were stored on my webserver.
I have since offloaded all of these PDFs to Box.net and embedded their widget in my downloads page.
Rather than have my web server use its resources to serve up these files, Box.net’s does all the hard work.
This seems to have saved considerable CPU usage,
Recommended Reading:
CDN
Offloading your static files like images, pictures and video to a Content Delivery Network like Amazon S3 can substantially speed up your blog.
W3 Total Cache also provides CDN functionality.
Database Maintenance
Your database stores everything.
And just like your hard drive, over time, it gets defragmented.. and requires maintenance so it works in an efficient and optimal state.
Optimize
Optimizing your database is very easy.
If you are using cPanel, here is how..
From your cPanel, under the Databases component.. click on phpMyAdmin,
Select your blog’s database on the left-hand-side,
Your database’s tables will now be displayed.
Scroll to the bottom of this table and click on Check tables having overhead,
In the drop-down list, click on Optimize table,
All tables that have overhead (i.e. de-fragmented) will now be optimized.
It is really up to you how often you do this.. I personally do it once a day.
WordPress Database Errors – MySQL Server has Gone Away
This error is very common amongst WordPress blogs that are on a shared server.. and probably 99% of bloggers are unaware of it.
If your blog experiences random timeouts and is extremely slow then you probably have a timeout issue with your database.
My blog was experiencing this for many months.. until I found a solution.
My post – Solution for “WordPress database error MySQL server has gone away for query” outlines the reasons why the error occurs and has a step-by-step walk-through on how to fix it.
Disable Post Revisions
WordPress has a feature called Post Revisions.
Every time you save a draft or publish a post, a revision copy is also made of that post.
So whenever a post is edited, a new row will be created in the wp_posts table. If your posts or pages get edited 10 times, you will have 10 new rows in wp_posts table!
It was introduced to help WordPress bloggers keep revisions of posts.. as well as restoring to previous revisions at anytime.
If you have a site with many articles, then Post Revision can take up a huge chunk of your database.
To turn off this feature.. copy and paste the following code into your wp-config.php file,
1 | define(‘WP_POST_REVISIONS’, false); |
Make sure you backup you database before executing any SQL queries..
To delete post revisions.. copy the following code,
1 2 3 4 5 | DELETE a,b,c FROM wp_posts a LEFT JOIN wp_term_relationships b ON (a.ID = b.object_id) LEFT JOIN wp_postmeta c ON (a.ID = c.post_id) WHERE a.post_type = ‘revision’ |
Head over to your phpMyAdmin interface.. select your database.. then click on the SQL tab,
Paste in the code.. then click <Go>,
You will see how many post revision rows have just been deleted,
Resizing Images
Another thing I did to help reduce my CPU usage was to resize ALL my images..
This is the process I followed,
- I downloaded all my images from ../wp-content/uploads/
- I immediately made a backup of these just in case something went wrong
- I used an image resizing tool to resize all of them.. ensuring that their filenames and file extensions remained the same
- I uploaded all the newly resized images back to their original locations on my webserver – ../wp-content/uploads/
I think they were about ~80MB in total.. and after the resize, they weighed in at ~40MB
So effectively, my CPU doesn’t have to work quite as hard when serving up all the images in my posts.
I couldn’t see any noticeable difference in my posts.. the image quality still looked the same.
I didn’t want to use a plugin like smush.it to do this job.. as the plug-in itself could have taken up valuable resources.
24 More WordPress Speed and Optimization Resources
- 8 Useful Tricks To Clean Up & Streamline Your WordPress Database – Make Tech Easier
- 10 Ways to Speed Up Your Blog – SEM Group
- 11 Ways to Speed Up WordPress – Mashable
- 13 Great WordPress Speed Tips & Tricks for MAX Performance – Coupe
- 18 Useful Tricks To Speed Up WordPress & Boost Performance – WP Beginner
- A Detailed Look Into WordPress Database Optimization – on extra pixel
- How to Optimize a WordPress Database – Pro Blog Design
- How to Speed up WordPress – Wolf Howl
- How to Speed Up Your WordPress Blog by Having Less Plugins – Web Tech Wise
- How to Survive a Good Slashdotting or DiGGing – One Man’s Blog
- Improve WordPress Site Speed To Improve Web Search Ranking – How to Spotter
- Leverage Browser Caching to Increase Website Speed – Quick Online Tips
- Optimize and Speed Up Your WordPress Blog – Daily Blog Tips
- Optimizing Your Website for Speed – Creative Fan
- Optimizing WordPress Blog for Speed – WP Web Host
- Page Speed – Google
- Reduce WordPress CPU Usage #1 – General tips – Craving Tech
- Significantly Speed Up Your WordPress Blog in 9 Easy Steps – Blogging Pro
- Speed Up WordPress – Ultimate Guide To Make Sites Super Fast – Computing Unleashed
- The 3 Easiest Ways to Speed Up WordPress – Lorelle on WordPress
- WordPress : 10+ life saving SQL queries – Cats Who Code
- WordPress Comments Slow Down Page Speed – SEO Moto
- WordPress Optimization Bible – Preloves
- Ultimate Guide To Web Optimization (Tips & Best Practices) – Hongqiao
The Need for Speed
Now that you have optimized your site for speed and performance, go back to the speed testing sites mentioned at the start of this post and re-test.
Upgrade Hosting Plan
If these tips don’t help to reduce your CPU utilization and it is still crossing 100%, you might have to bite the bullet and upgrade your hosting plan.
If you are at the highest plan within a shared server environment and you still need more resources.. the next option you have is to upgrade to a Virtual Private Server.
After that you could go onto a Dedicated Server..
As you know.. the higher and more specialized the service.. the more money you will be paying.