Optimizing WordPress

P3 Results StackedI am very impressed with WordPress, and over the past several years it has become my web platform of choice, but the fact is, more and more I find myself in the position of trying to find ways to make it more efficient. I don’t think this has anything to do with WordPress itself. Installed alone (no plugins) with a basic theme, it’s pretty efficient, but who wants to use it that way? Not my clients. They want as many bells and whistles as they can get, and that means a customized theme, plenty of jQuery and javascript, lots of images, and all kinds of plugins. Overloading an unoptimized WordPress site can lead to a suspended account on a shared hosting service, sluggish site loading, and users abandoning the site because it responds too slow.

My purpose today is to share some of the methods I’ve found that really work to optimize WordPress, reduce the load on the server, increase the performance of a site, and improve the user experience.

Before you get started, you may want to install GoDaddy’s P3 (Plugin Performance Profiler) and run a baseline test so you can see the benefits of each optimization step.

Step One: Configuration

The place to start optimizing WordPress is in wp-config.php, the WordPress configuration file. Ideally, you would start out with these settings, but all of them can be initiated at any time.

Don’t Save Revisions

By default, WordPress saves all your post and page revisions as individual entries in the wp_posts table in your database. The larger a database table, the longer it will take MySQL to wade through it. The longer MySQL takes to process a query, the longer it will take for your site to load a page. That’s not good. Unless you absolutely need all those revisions, turn them off completely, or at least limit them.

To do so, edit the file wp-config.php. Find the line:

define('WP_DEBUG', false);

Below it, add the following:

// Limit revisions.
define('WP_POST_REVISIONS', 0);

Setting WP_POST_REVISIONS to zero will make WordPress stop saving any revisions except for one autosave per post. You can replace the “0” with any integer value to keep more revisions. Older revisions are automatically deleted going forward, though if you’re working with an existing site that already has a bunch of revisions stored in the wp_posts table, you will need to delete them manually.

Turn Off Automatic Updates

This is more of a preference than a true optimization step; however, my point in suggesting you turn off automatic updates is that it will prevent any updates from happening without your knowledge and at a time when you aren’t prepared with any issues that might arise. If you do disable automatic updates, you should be sure to monitor your sites to keep them up-to-date; failing to do so puts them at risk of security breaches.

To turn off automatic updates, add the following in wp-config.php below your WP_POST_REVISIONS entry:

define( 'AUTOMATIC_UPDATER_DISABLED', true );

Disable WP_CRON

WordPress is designed to work well on a variety of web hosts. Some don’t offer the end user access to cron, the time-based job scheduler on most hosting platforms, so by default WordPress triggers the script wp-cron.php on each page access. Even though the WordPress developers have done a good job in making wp-cron.php efficient, if you are able to set up cron jobs, you can take a bit of the load off your server by turning off wp-cron.php and firing it via a real cron job.

To turn off WordPress’ default wp-cron.php behavior, add the following to wp-config.php after the AUTOMATIC_UPDATER_DISABLED line you added above (or after WP_POST_REVISIONS if you elected not to turn off automatic updates):

define('DISABLE_WP_CRON', true);

Those are all the changes to wp-config.php. Upload the changed file to your site.

Next, you have to create the cron job to run wp-cron.php. The method for creating the cron job will differ depending on the user interface your web host offers to handle cron. If you’re using a host that uses cPanel, here’s how it’s done:

  1. Log on to your cPanel control panel.
  2. Scroll down to the Advanced section.
  3. Click on the Cron jobs link.
  4. Make sure you have a valid email address entered in the Cron Email section; that email address is used to notify you in the event a cron job fails.
  5. In the Add New Cron Job section, select “Once an hour” from the drop-down.
  6. Pick a value in the Minute field that will be likely to reduce the stress on the server; most people tend to run cron jobs at zero minutes or at 5, 10, 15… minute intervals. Pick something you think will put this cron job at a less-used interval.
  7. In the command box, enter the following, modifying the path to your wp-cron based on the user name for your hosting account and the subfolder–if any–in which you have installed WordPress:
    /usr/bin/php /home/username/public_html/wp-cron.php >/dev/null 2>&1
  8. Click the Add New Cron Job button to save your changes.

 Step Two: Install a Caching Plugin

All these optimization steps have a cumulative effect, but installing a good caching plugin makes the biggest improvement in the performance of a modern WordPress site. If you do nothing else to optimize your WordPress installation, you should do this.

There are a number of caching plugins out there; my favorite right now is WP Super Cache. I have found it to be the most trouble-free, least buggy, and best-performing of those I have tested.

Notes: You should only have one caching plugin installed and activated at a time! Your server must be running mod_rewrite for the instructions below to work. Most Linux-based hosts using Apache or LiteSpeed run mod_rewrite by default. If in doubt, contact your web host.

To install and configure it:

  1. Log in to your WordPress admin.
  2. Go Plugins -> Add New.
  3. Type “wp super cache” into the search box and click Search.
  4. Click the “install now” link for WP Super Cache.
  5. After the installation completes, click “Activate the plugin.”
  6. Now you must configure WP Super Cache. Go Settings -> WP Super Cache (or click on the plugin admin page link in the message to go to WP Super Cache’s Settings page that may appear at the top of your screen after activating the plugin).
  7. Click on the Advanced tab.
  8. Check the box next to “Cache hits to this website…”
  9. Select the button next to “Use mod_rewrite to serve cache files.”
  10. Check the box next to “Compress Pages.”
  11. Check the box next to “304 Not Modified browser caching.”
  12. Check the box next to “Don’t cache pages for known users.”
  13. “Cache rebuild” and “Extra homepage checks” should already be checked.
  14. Click the Update Status button.
  15. Scroll down and select “Update Mod_Rewrite Rules” in the yellow box.
  16. Scroll down to “Expiry Time & Garbage Collection.”
  17. Make sure “3600” is entered in the the Cache Timeout box.
  18. Make sure “Timer” is selected for Scheduler.
  19. Click the Change Expiration button.

That’s it. You’re done. You’ll find that while you are working on your site, logged in to the admin side, you see all your current changes, but if you log out, you may not. When you’re done working on the site, but before you log out, go Settings -> WP Super Cache and click on the Content tab. Click the button to delete the cache. Now when you log out, all your recent changes should be visible.

There is a “Delete Cache” link added to the main toolbar in WordPress, but I have found that it doesn’t always clear the cache as effectively as going to the Content page and doing so.

Step Three: Tweak Your robots.txt File

Search engines and other ‘bots can create a lot of traffic on a web site. Sometimes, their enthusiastic crawling of your site can lead to server overload. Although the nastier ones will ignore any settings in your robots.txt file, the major search engines (except for Google) will heed any applicable settings.

To slow down the rate at which the ‘bots crawl your site, put the following at the very top of your robots.txt file:

User-agent: *
Crawl-delay: 5

You can substitute any integer value for the “5” in the code above, though five is a good place to start. The higher the value, the lower the load on your server; however, there is some anecdotal evidence that too high a value may hurt your search engine rankings by discouraging the ‘bots from spending time on your site. They apparently want to grab the data and move on quickly.

 Step Four: Optimize Your Plugins and Theme

Optimizing the plugins and theme your WordPress site uses is a much easier job than it used to be. I wrote in a previous post about GoDaddy’s free P3 (Plugin Performance Profiler).

I recommend installing the P3 plugin on any site you want to optimize, then running it and taking a look at the Detailed Breakdown tab. The faster your site loads, the better. You should aim for something as far below two seconds as you can get; anything longer than two seconds, and your site abandonment rate will go way up.

The P3 plugin will show you how long components of your site, including the plugins you have activated and your current theme, are taking to load. You can easily identify the resource hogs and either remove them or find others that are more efficient at accomplishing the same tasks.

Even with the P3 plugin, this process takes some time, but with it you have some objective data to use to determine where the problems are.