WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

WordPress Optimization

Whether you run a high traffic WordPress installation or a small blog on a low cost shared host, you should optimize WordPress and your server to run as efficiently as possible. This article provides a broad overview of WordPress optimization with specific recommended approaches. However, it's not a detailed technical explanation of each aspect.

If you need a quick fix now, go straight to the Caching section, you'll get the biggest benefit for the smallest hassle there. If you want to get started on a more thorough optimization process immediately, go to How Do You Improve Performance in WordPress.

A broad overview of the topic of performance is included below in What Affects Performance and How Do You Measure Performance. Many of the techniques discussed here also apply to WordPress Multisite (MU).

What Affects Performance

Hosting

The optimization techniques available to you will depend on your hosting setup.

Shared Hosting

This is the most common type of hosting. Your site will be hosted on a server along with many others. The hosting company manage the web server for you, so you have very little control over server settings and so on. The areas most relevant to this type of hosting are: Caching, WordPress Performance and Offloading

Virtual Hosting and Dedicated Servers

With this hosting scenario, you have control over your own server. The server might be a dedicated piece of hardware or one of many virtual servers sharing the same physical hardware. In addition to the areas above (caching and WordPress performance, the key areas of interest here are: Server Optimization and * Offloading

Number of Servers

Once you're dealing with very high traffic situations it may be necessary to employ multiple servers. If you're at this level, you should already have employed all of the applicable techniques listed above.

The WordPress database can be easily moved to a different server and only requires a small change to the config file. Likewise images and other static files can be moved to alternative servers (see offloading).

Amazon's Elastic Load Balancer can help spread traffic across multiple web servers but requires a higher level of expertise. If you're employing multiple database servers, the HyperDB class provides a drop-in replacement for the standard WPDB class and can handle multiple database servers in both replicated and partitioned structures.

Hardware Performance

Your hardware capability will have a huge impact on your site performance. The number of processors, the processor speed, the amount of available memory and disk space as well as the disk storage medium. Hosting providers generally offer higher performance for a higher price.

One innovation in hosting is the use of Solid State Drives or SSDs which are faster than traditional magnetic media. Digital Ocean offers affordable SSD hosting and Amazon has recently added SSD hosting to its cloud offerings.

WordPress Configuration

Your theme will have a huge impact on the performance of your site. A fast, lightweight theme will perform much more efficiently than a heavy graphic-laden inefficient one.

The number of plugins and their performance will also have a huge impact on your site's performance. Deactivating and deleting unnecessary plugins is very important way to improve performance.

Software performance

Making sure you are using the latest software is also important - as software upgrades often fix bugs and enhance performance. Making sure you're running the latest version of Linux (or Windows), Apache, MySQL/MariaDB and PHP is very important.

Keeping up with WordPress upgrades is also important.

Server Load

The amount of traffic on your server and how its configured to handle the load will have a huge impact as well. For example, if you don't use a caching solution, performance will slow to a halt as additional page requests come in and stack up, often crashing your web or database server. If configured properly, most hosting solutions can handle very high traffic. Offloading traffic to other servers can also reduce server load.

Abusive traffic such as login attacks, image leaking (other sites linking to your image files from high traffic pages) or DoS attacks can also increase server load. Identifying and blocking these attacks is very important.

Size of Graphics

Making sure the images in your posts are optimized for the web can save time, bandwidth and increase your search engine ranking.

Geographical distance

The distance between your server and your website visitors also has an impact on perceived performance. A Content Delivery Network or CDN can mirror static files and images across various geographic regions so that all of your site visitors have optimal performance.

How Do You Measure Performance?

  • Google PageSpeed is an excellent way to measure your WordPress site's performance and receive clear, specific feedback on how to make improvements.
  • New Relic also provides a free library for measuring server performance in PHP and MySQL/MariaDB
  • Linux Top provides a dashboard for your server's real time performance.
  • If using Varnish Cache, VarnishStat helps assess the performance aspects of your caching tools.
  • Zend Server Z-Ray provides X-Ray vision and deep insight into your WordPress PHP application

Tools for Performance Testing

How Do You Improve Performance in WordPress

If you need a quick fix now, go straight to the Caching section, you'll get the biggest benefit for the smallest hassle there.

Optimizing Your WordPress Configuration

Minimizing Plugins

The first and easiest way to improve WordPress performance is plugins. Deactivate and delete any unnecessary plugins. Try selectively disabling plugins to measure server performance. Is one of your plugins significantly affecting your site's performance?

Then you can look at optimizing plugins. Are plugins coded inefficiently? Do they repeat unnecessary database queries? WordPress has its own caching system, so generally speaking, using functions like get_option(), update_option() and so on will be faster than writing SQL.

Themes

After plugins come theme optimization.

  • Image Files
    • Are there any unnecessary images? (e.g. Can you replace some of the images with text?)
    • Make sure all image files are optimized. Choose the correct format (JPG/PNG/GIF) for the type of image.
  • Total File Number/Size
    • Can you reduce the number of files needed to display the average page on your site?
    • Combine multiple CSS files into a single, optimized file.
    • Minify CSS and JavaScript files.
    • If necessary, look into plugins to help this process.
  • Query Reduction/Optimization
    • Can static values be hardcoded into your themes? This will mean you have to edit code every time you make changes, but for generally static areas, this can be a good trade off.
      • For example, your site charset, site title, and so on.
      • Can you hardcode menus that rarely change? Avoiding functions like wp_list_pages() for example.

We have seen themes which put 3x extra load to the server. Turned out it caused 3x more database queries - which is bad in itself. Then we found out that some of the queries are unoptimized. Not good.

You can also use offloading to optimize your theme.

Let Someone Else Do It

Using a Managed Hosting solution such as WordPress.com, Pagely or WPEngine takes advantage of the hard work and expertise of service providers to do the heavy lifting of optimization for you.

Upgrade Hardware

Paying more for higher service levels at your hosting provider can be very effective. Increasing memory (RAM) or switching to a host with Solid State Drives (SSD) e.g. Digital Ocean can make a big difference. Increased number of processors and processor speed will also help.

Optimize Software

Make sure you are running the latest operating system version e.g. Linux, Windows and the latest web server e.g. Apache, database e.g. MySQL server and PHP.

DNS: Don't run a DNS on your WordPress server. Use a commercial service for DNS such as Amazon's Route 53 or your domain registrar's free offering. Using a service such as Amazon can also make switching between backup servers during maintenance or emergencies much easier. It also provides a degree of fault tolerance. If you host your DNS on external servers this will reduce the load on your primary web server. It's a simple change, but it will offload some traffic and cpu load.

Web Server: Your web server can be configured to increase performance. There are a range of techniques from web server caching to setting cache headers to reduce load per visitor. Search for your specific web server optimizations (for example, search for "apache optimization" for more info). Some web servers have higher speed versions you can pay for such as Apache Litespeed. There are also a number of ways to tune Apache for higher performance based on your particular hosting and site configuration, e.g. Memcache.

PHP: There are various PHP accelerators available which can dramatically improve performance of your PHP files. This will apply to all PHP files, not just your WordPress installation. Search for PHP optimization for more information, f.e. APC.

MySQL/MariaDB: MySQL or MariaDB optimization is a black art in itself. A few simple changes to the query cache settings can have a dramatic effect on WordPress performance because WordPress repeats a lot of queries on every request. Search for mysql optimization for more.

A great example of how WordPress has been optimized was presented by Iliya Polihronov at WordCamp San Francisco 2012. Iliya, among other things, does server optimization for WordPress.com.

Don't run a mail server on your WordPress server. For your contact form, use something like Contact Form 7 with free Mailgun.

Caching

Caching Plugins

Caching plugins can be easily installed and will cache your WordPress posts and pages as static files. These static files are then served to users, reducing the processing load on the server. This can improve performance several hundred times over for fairly static pages.

When combined with a system level page cache such as Varnish, this can be quite powerful. If your posts/pages have a lot of dynamic content configuring caching can be more complex.

Server-side Caching

Caching data at the server level is more complex than using basic WordPress cache plugins, but this approach is generally much more effective, and is therefore used widely among high traffic websites. The simpler solutions cache data locally on the origin server, while more complex and involved systems may use multiple caching servers (also known as reverse proxy servers) that are placed "in front" of the web server where the WordPress installation is actually running.

OPcode Caching (PHP OPcache)

Because WordPress is PHP-based, it contains a large amount of code that must be "parsed" by PHP on every single page load; if you are using a heavy theme or plugin, or a large number of plugins, the amount of PHP code grows exponentially.

In many server environments (especially shared hosting), this "parse" time can greatly affect performance, adding several seconds to the total time it takes to finish loading a page. This is where OPcode caching can help, sometimes drastically, as it avoids the need to (re)parse PHP code too often, by way of "caching" the PHP content in a temporary cache.

Throughout its lifetime, PHP has supported various OPcode caching extensions. For many years, the most popular one was called Zend, which was a proprietary third party script, followed by APC, which was also maintained by a third party. However, upon the release of PHP 5.5, Zend decided to open-source their code and contribute it entirely to the PHP project, at which point it was included (and enabled by default) as part of PHP installations.

OPcache improves PHP performance by storing precompiled script bytecode in shared memory, thereby removing the need for PHP to load and parse scripts on each request. -- PHP.net

This is one of the reasons why it is so important to upgrade your PHP version. Many web hosts allow you to upgrade by request, while others do it automatically. And while OPcache is supported by PHP 5.5+ it has several more options available in PHP 7+ (along with generally faster PHP performance), so it's a good idea to stay as updated as possible.

Memory-based Caching

  • Varnish: stores pre-built pages in memory and serve them quickly without requiring execution of the Apache, PHP, WordPress stack. As described within, using a plugin for comments such as Disqus instead of native WordPress comments can assist Varnish by not requiring your readers to login to WordPress and increasing the number of page views that Varnish can serve out of the cache.

Browser Caching

Browser caching can help to reduce server load by reducing the number of requests per page. For example, by setting the correct file headers on files that don't change (static files like images, CSS, JavaScript etc) browsers will then cache these files on the user's computer. This technique allows the browser to check to see if files have changed, instead of simply requesting them. The result is your web server can answer many more 304 responses, confirming that a file is unchanged, instead of 200 responses, which require the file to be sent.

Look into HTTP Cache-Control (specifically max-age) and Expires headers, as well as Entity Tags for more information.

Further Reading

Offloading

Use a content Delivery Network (CDN)

Using a CDN can greatly reduce the load on your website. Offloading the searching and delivery of images, javascript, css and theme files to a CDN is not only faster but takes great load off your WordPress server's own app stack. A CDN is most effective if used in conjunction with a caching plugin.

Cloudflare is a popular Content Delivery Network, which also offers Internet Security services. Plans start from free, but additional features are available for extra costs. Cloudflare is a fixed-cost CDN, meaning they charge by features instead of usage. Cloudflare allows you to route your sites traffic through their network before coming back to your origin host. On Cloudflare's Knowledge Base they have have a guide on how to Speed Up WordPress and Improve Performance whilst using their service.

Amazon Cloudfront uses the Amazon S3 service to provide Content Delivery Network (CDN) functionality for your static files. A CDN is a service which caches your static files on numerous web servers around the world. Providing faster download performance for your users no matter where they are. It's recommended that you use Cloudfront in tandem with S3 and not only S3 alone; the costs are not significantly different. MaxCDN is a pay-per-usage Content Delivery Network (CDN) similar to Amazon Cloudfront. Among the differences are support for Video-on-demand as well as "mirroring" (no uploading required) of files, although you can upload them if you prefer.

There are other CDN providers out there, so do some online searching, and you will more that may suit your needs.

Static Content

Any static files can be offloaded to another server. For example, any static images, JavaScript or CSS files can be moved to a different server. This is a common technique in very high performance systems (Google, Flickr, YouTube, etc) but can also be helpful for smaller sites where a single server is struggling. Also, moving this content onto different hostnames can lay the groundwork for multiple servers in the future.

Some web servers are optimized to serve static files and can do so far more efficiently than more complex web servers like Apache, for example lighttpd.

Amazon Simple Storage Service (S3) is a dedicated static file hosting service on a pay-per-usage basis. With no minimum costs, it might be practical for lower traffic sites which are reaching the peak that a shared or single server can handle.

Multiple Hostnames

There can also be user improvements by splitting static files between multiple hostnames. Most browser will only make 2 simultaneous requests to a server, so if you page requires 16 files they will be requested 2 at a time. If you spread that between 4 host names they will be requested 8 at a time. This can reduce page loading times for the user, but it can increase server load by creating more simultaneous requests. Also, known is "pipelining" can often saturate the visitor's internet connection if overused.

Offloading images is the easiest and simplest place to start. All images files could be evenly split between three hostnames (assets1.yoursite.com, assets2.yoursite.com, assets3.yoursite.com for example). As traffic grows, these hostnames could be moved to your own server. Note: Avoid picking a hostname at random as this will affect browser caching and result in more traffic and may also create excessive DNS lookups which do carry a performance penalty.

Likewise any static JavaScript and CSS files can be offloaded to separate hostnames or servers.

Feeds

Your feeds can quite easily be offloaded to an external service. Feed tracking services like Google FeedBurner will do this automatically, the Feedburner servers will handle all the feed traffic and only update the feed from your site every few minutes. This can be a big traffic saver.

Likewise you could offload your own feeds to a separate server (feeds.yoursite.com for example) and then handle your own feed stats / advertising.

Free Hosting Services

It may be possible to host some of your files on external servers for free. For example, popular image hosting web sites like Flickr provide image hosting at no cost. Even offloading your most popular images to a free service could significantly reduce the impact on your main server.

However, there is a very important issue to consider when using a free service to offload your images. Since most of the popular services are actually photo sharing sites, copyright becomes a concern. Be sure to read the service disclaimer and decide if it suits you. Moreover, you may not want your images exposed to the users of the photo sharing site.

Consider that free image hosting services are recently becoming a less viable choice since traffic from them is often blocked by corporate networks.

Further Reading

Database Tuning

Cleaning Your Database

Many plugins can help you reduce extra clutter in your database.

You can also instruct WordPress to minimize the number of revisions that it saves of your posts and pages.

Additional Resources

Further Reading

WordCamp Performance Presentations