How to Optimize Performance & PageSpeed Score of a WordPress Blog

6

Hello Friends, Here is the most asked question across bloggers about WordPress Optimizations. Here we will do it with one plugin named “W3 Total Cache” which is already famous among bloggers and CloudFlare which is known for its optimization and security and at last we will minify some unoptimized files manually. Here with the combination of these three tools we are going for WordPress Optimization.

Read: Best Tips To Boost Alexa Rank

Steps for Performance Optimizations on WordPress

For Best Optimisations Result I will recommend you all to use Cloudflare along with W3 Total Cache with the settings that are specified below:-

Moving your DNS and Name Servers to Cloudflare

So, friends, this is the easiest and interesting step in this process. You can read the whole process for this here Boost blog speed.

Setting up WordPress Caching Mechanism

WordPress Optimizations

Setting Up W3 Total Cache

First, you have to install W3 Total Cache from the WordPress Repository. Now after this the most preferred settings are illustrated with its state i.e. Enabled or Disabled.

PS: WP Rocket is one of the best alternatives of W3 Total Cache as there are not of advanced configuration there(Everything is being already taken care of by the plugin team). It will be a very easy setup.

Go to General Settings from Performance tab in WordPress admin panel.

  1. Toggle all caching types on or off:- Disabled.
  2. Page Cache:- Enabled.
  3. Page Cache Method:- Choose according to your hosting specification. (Default:- Disk Enhanced)
  4. Minify:- Disabled. (Due to disabling no further setting change in sub-items of minifying)
  5. Database Cache:- Enabled.
  6. Database Cache Method:-  Choose according to your hosting specification. (Default:- Disk)
  7. Object Cache:- Disabled.
  8. Browser Cache:- Enabled.
  9. CDN:- Enable if you use any external CDN Service. (Default:- Disabled)
  10. Enable Varnish Cache Purging:- Disabled.
  11. CloudFlare:- Enabled. Enter Email Address that is registered with CloudFlare, Enter API Key and Domain without www.
  12. Security Level:- Medium.
  13. Rocket Loader:- Automatic. (Load All JavaScript Resources Async. i.e. Large Performance Boost )
  14. Minification :- HTML Only.(Safe)
  15. Development Mode:- Off.
  16. New Relic:- Enable if use New Relic. (Default:- Disabled)
  17. Miscellaneous: Verify rewrite rules:- Enabled.

Go to Page Cache from Performance tab in WordPress admin panel.

  1. Cache front page:- Enabled.
  2. Cache feeds: site, categories, tags, comments:- Enabled.
  3. Cache SSL (https) requests :- Disabled.
  4. Cache URIs with query string variables:- Disabled.
  5. Cache 404 (not found) pages:- Disabled.
  6. Cache requests only for www.*.com site address:- Enabled.
  7. Don’t cache pages for logged in users:- Enabled.
  8. Automatically prime the page cache:- Enabled.
  9. Update Interval:- 900 Seconds.
  10. Pages per interval:- As your server limit.(Default :- 15)
  11. Pre-load the post cache upon publish events:- Enabled.
  12. Leave other settings to default.

Go to Database Cache from Performance tab in WordPress admin panel.

  1. Don’t cache queries for logged in users:- Enabled.
  2. Maximum lifetime of cache objects:- 180000000 seconds.
  3. Garbage collection interval :- 360000000 seconds.
  4. Leave other settings to default.

Go to Browser Cache from Performance tab in WordPress admin panel.

General

Viglink

  1. Set cache control header:- Enabled.
  2.  :- Disabled.
  3. Set W3 Total Cache header:- Enabled.
  4.  :- Enabled.
  5. Prevent caching of objects after settings change:- Disabled.
  6. Disable cookies for static files:- Disabled.
  7. Do not process 404 errors for static objects with WordPress:- Disabled.

CSS & JS

  1.  :- Enabled.
  2. Set expires header:- Enabled.
  3. Expires header lifetime:- 31536000 seconds.
  4. Set cache control header:- Enabled.
  5. Cache-Control policy:- Cache with max age.
  6.  :- Disabled.
  7. Set W3 Total Cache header:- Enabled.
  8. Enable HTTP (gzip) compression :- Enabled.
  9. Prevent caching of objects after settings change:- Disabled.
  10. Disable cookies for static files:- Enabled.

HTML & XML

  1.  :- Enabled.
  2. Set expires header:- Enabled.
  3. Expires header lifetime:- 3600 seconds.
  4. Set cache control header:- Enabled.
  5. Cache-Control policy:- Cache with max age.
  6.  :- Disabled.
  7. Set W3 Total Cache header:- Enabled.
  8.  :- Enabled.

Media & Other Files

  1. Set Last-Modified header:- Enabled.
  2. Set expires header:- Enabled.
  3. Expires header lifetime:- 31536000 seconds.
  4. Set cache control header:- Enabled.
  5. Cache-Control policy:- Cache with max age.
  6.  :- Disabled.
  7. Set W3 Total Cache header:- Enabled.
  8. Enable HTTP (gzip) compression :- Enabled.
  9. Prevent caching of objects after settings change:- Disabled.
  10. Disable cookies for static files:- Disabled.

Leverage Browser Caching Using .htaccess (Alternative Method)

Warning: Before you start editing .htaccess file, make sure to back it up, In case something goes wrong you can restore it back to make your blog accessible again. 

  1. The first step is to find your .htaccess file which is located in the root directory of your WordPress installation.
  2. Take a backup of your .htaccess file, in case something goes wrong you can replace the file to keep everything working.
  3. Open up .htaccess file.
  4. The first thing that you need to do is to get rid of Etag. This is because after disabling Etag, a browser needs to rely on cache-control and expires header that is defined by you. For doing so add the following code to your .htaccess file:
    Header unset Pragma
    FileETag None
    Header unset ETag
  5. Now after disabling Etag. You need to specify expiry headers for the static content like CSS, JS, images, PDFs etc. For that add the following code:
    ## EXPIRES CACHING ##
    ExpiresActive On
    ExpiresByType image/jpg "access 1 year"
    ExpiresByType image/jpeg "access 1 year"
    ExpiresByType image/gif "access 1 year"
    ExpiresByType image/png "access 1 year"
    ExpiresByType text/css "access 1 month"
    ExpiresByType application/pdf "access 1 month"
    ExpiresByType text/x-javascript "access 1 month"
    ExpiresByType application/x-shockwave-flash "access 1 month"
    ExpiresByType image/x-icon "access 1 year"
    ExpiresDefault "access 2 days"
    ## EXPIRES CACHING ##
  6. Further, you can optimize the content delivery by compressing components of your pages. For this, to work you need to have mod_deflate or mod_gzip installed on your hosting server. Now add the following code:
    mod_deflate
    <FilesMatch "\\.(js|css|html|htm|php|xml)$">
    SetOutputFilter DEFLATE
    </FilesMatch>
    
    mod_gzip
    <IfModule mod_gzip.c>
    mod_gzip_on Yes
    mod_gzip_dechunk Yes
    mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
    mod_gzip_item_include handler ^cgi-script$
    mod_gzip_item_include mime ^text/.*
    mod_gzip_item_include mime ^application/x-javascript.*
    mod_gzip_item_exclude mime ^image/.*
    mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
    </IfModule>
  7. Now save your .htaccess file.

Optimizing JavaScript & CSS Manually

In this step, we will manually minify the JavaScript which is not automatically optimized by W3 Total Cache And CloudFlare. You can also minify CSS using the same steps as Below but many times it can also break your theme so do it at your own risk.

Identifying and Minifying JavaScript/CSS using GTMetrix

  1. Go to GTmetrix.
  2. Analyze your blog.
  3. In Pagespeed find Minify Javascript.
  4. Expand it. Now you have to see the location of that JavaScript file on your blog.
  5. Click Optimised version to see the Optimised JavaScript File.
  6. Go to the file manager to the location of the JavaScript file. Click edit/code edit.
  7. Replace all the content with GTmetrix Optimised content.
  8. Click Save.
  9. Repeat this process for all other local JavaScript Files & do the same for CSS files.

Fix Render-Blocking JavaScript & CSS

  1. Go to WordPress Dashboard -> Performance -> General Settings.
  2. Change Minify mode to manual.
  3. Go to Google Pagespeed Insights and test your URL. Keep this page open we will need it later.
  4. Go to WordPress Dashboard -> Performance -> Minify.
  5. Navigate to JS section and enable it & change embed type of Before </head> to Non-blocking using “async”.Fix Render-Blocking JavaScript & CSS in WordPress
  6. Get URL of the JS files that need to be fixed from Google Pagespeed Insight test we did earlier.
  7. Now one by one add those files as shown in the screenshot below.Fix Render-Blocking JavaScript & CSS in WordPress
  8. After adding all files click Save all settings.
  9. Similarly, Enable CSS to minify settings.Fix Render-Blocking JavaScript & CSS in WordPress
  10. In CSS File management follow the same process we did in JS above.Fix Render-Blocking JavaScript & CSS in WordPress
  11. After adding all files click Save all settings.
  12. Now Empty all caches and check Google PageSpeed Insights score again.

Caution: Adding some JS/CSS file may cause a problem in loading or undesirable user experience. So you can check after adding each file whether it is compatible with this optimization or not.

Deferring Parsing of JavaScript using Functions.php

Warning: Before you start editing the functions.php file of your theme, make sure to back it up, In case something goes wrong you can restore it back to make your blog working. 

  1. Open up the functions.php file in your theme folder either via hosting panel or WordPress editor.
  2. Add up the following code in the functions.php file.
  3. function defer_parsing_of_js ( $url ) {
    if ( FALSE === strpos( $url, '.js' ) ) return $url;
    if ( strpos( $url, 'jquery.js' ) ) return $url;
    return "$url' async onload='myinit()";
    }
    add_filter( 'clean_url', 'defer_parsing_of_js', 11, 1 );
  4. Save your files and check your blog performance.
  5. In most of the cases this works great, but if this is breaking your theme, you should remove the added code to revert.

Combine Images using CSS Sprites

  1. Open SpriteMe(Discontinued) [Use this instead].
  2. After opening, you will see the installation section and drag that link to your bookmark toolbar. (It is actually a JavaScript)
  3. Open up your blog in a browser.
  4. Click SpriteMe from the bookmark bar. You will find something like this.Combine Images Using CSS Sprites To Boost Blog Speed
  5. Find the images that are needed to optimize using GTmetrix.
  6. According to your requirement drag and drop to include or not include an image.
  7. Finally, click make sprite.
  8. Sprite will be created within few seconds and click on the link to open the sprite and save it.
  9. Now click export CSS. I will advise you to save the sprited image on your server.
  10. Do the changes as illustrated by SpriteMe in your CSS file and replace the URL from SpriteMe to the URL of the file you have copied on your server.
  11. Now you have successfully combined images using CSS sprites.

Comparison Between Before and After Optimisations

WordPress Optimizations
Pingdom Report Before Optimisations
WordPress Optimizations
Pingdom Report After Optimisations
WordPress Optimizations
GTmetrix Report Before Optimisations
WordPress Optimizations
GTmetrix Report After Optimisations

Thank you for reading this post and keep connected to Tech Arrival to continue enjoying wonderful posts. Share your views about this post in the comment section below. If you are still having any query throw it in the comment section.

If you find this post helpful, don’t hesitate to share this post on your social network. It will only take a moment and also it is quite easy and helpful for others.

  • Many of the settings depend very much on server configuration. Though this is a way to go. Probably only Cloud flare can be a week point, sometimes Cloud flare instances would slow down loading instead of speeding it up.

    • You are right but this happens in very few case, but it is still a good option. You can also check out page speed of Tech Arrival.