Optimize WordPress Website without Plugin
You should optimize your WordPress website for speed. It is an important SEO factor as well as provides a good user experience to your audience. In this tutorial, you will learn how to optimize your WordPress website for speed. There are many optimization Plugins for compressing images, caching and minify, etc. but we want to do it without any Plugin. A plugin is a software consisting of a group of functions to add functionality to the WordPress website. Using Plugins has pros and cons. The advantage is that you accomplish the task without knowing the coding. Advanced WordPress developers don’t prefer using Plugin for each job. Every Plugin, besides its main functionality, will also have extra CSS and JavaScripts to provide a user interface. It also provides a security threat to your WordPress website. Plugin code is available to hackers also, and they can very easily tweak its code for malicious activities. Many advanced WordPress developers, therefore, very rarely use Plugins.
It is advised to backup your WordPress website before making these changes.
Step 1: Cache Static Resources
Static resources are those that undergo changes after a very long time or don’t change at all. These are images, JavaScripts, and CSS files. Paste the following piece of code in your .htaccess file located in your Public_hmtl folder.
If you are using a cache Plugin, then rename your .htacess file to something like .htaccess-old, etc. and then log in to your WordPress dashboard. Hover to permalink and click on save. This way, you will generate a new .htaccess file. We have done this step to remove the modification done to .htaccess file by optimization Plugin.
# BEGIN Expire headers
ExpiresActive On
ExpiresDefault “access plus 5 seconds”
ExpiresByType image/x-icon “access plus 2592000 seconds”
ExpiresByType image/jpeg “access plus 2592000 seconds”
ExpiresByType image/png “access plus 2592000 seconds”
ExpiresByType image/gif “access plus 2592000 seconds”
ExpiresByType application/x-shockwave-flash “access plus 2592000 seconds”
ExpiresByType text/css “access plus 604800 seconds”
ExpiresByType text/javascript “access plus 216000 seconds”
ExpiresByType application/javascript “access plus 216000 seconds”
ExpiresByType application/x-javascript “access plus 216000 seconds”
ExpiresByType text/html “access plus 600 seconds”
ExpiresByType application/xhtml+xml “access plus 600 seconds”
# END Expire headers
# BEGIN Cache-Control Headers
<filesMatch “\.(ico|jpe?g|png|gif|swf)$”>
Header set Cache-Control “public”
<filesMatch “\.(css)$”>
Header set Cache-Control “public”
<filesMatch “\.(js)$”>
Header set Cache-Control “private”
<filesMatch “\.(x?html?|php)$”>
Header set Cache-Control “private, must-revalidate”
# END Cache-Control Headers
Step 2: Apply GZIP Compression
- Gzip is a method of making files smaller via compression to be delivered faster on networks.
- It is also a file format. GZIP compression will reduce the page size of your WordPress website. To compress the file without any Plugin, paste the following piece of code into your .htaccess file below the code inserted before this step.
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
AddOutputFilterByType DEFLATE application/x-font
AddOutputFilterByType DEFLATE application/x-font-opentype
AddOutputFilterByType DEFLATE application/x-font-otf
AddOutputFilterByType DEFLATE application/x-font-truetype
AddOutputFilterByType DEFLATE application/x-font-ttf
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE font/opentype
AddOutputFilterByType DEFLATE font/otf
AddOutputFilterByType DEFLATE font/ttf
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE image/x-icon
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml
</IfModule>
Step 3: Limit the Number of Post Revisions
By default, WordPress stores every change we make to our pages and posts. It is called revisions. They are stored separately in our WordPress database. We can reduce our database size if we limit the number of post revisions or remove them.
To limit the number of post revision to 2, paste the following piece of code in wp-config.php.
define( ‘WP_POST_REVISIONS’, 2);
If we want to not store post revisions at all, then we will replace two by false.
Step 4: Defer JavaScript
According to javascript.info:
The defer attribute tells the browser that it should go on working with the page, and load the script “in the background,” then run the script when it loads. Scripts with deferring never block the page. They always execute when the DOM is ready but before the DOMContentLoaded event.
To defer JavaScript, paste the following piece of code in your functions.php file.
function defer_parsing_of_js( $url ) {
if ( is_user_logged_in() ) return $url; //don’t break WP Admin
if ( FALSE === strpos( $url, ‘.js’ ) ) return $url;
if ( strpos( $url, ‘jquery.js’ ) ) return $url;
return str_replace( ‘ src’, ‘ defer src’, $url );
}
add_filter( ‘script_loader_tag’, ‘defer_parsing_of_js’, 10 );
Here is the final result after applying all these steps on my website. The result can further be improved. You can see in the following picture that TTFB (time to the first byte) is 1 second. It is because I am on cheap shared hosting. The load time can be brought to under one second if I shift to better hosting. I have now started this blog, so I am on cheap hosting. I will migrate to a higher plan soon as I see an increase in traffic.
Conclusion
I have applied these steps and improved page speed. Google Analytics Tags and Google Adsense decrease the page speed score, but I was still able to achieve good results. Also, I am on shared hosting.
These steps will increase your page speed in the Google Page Speed test and GTmetrix both. If you have any trouble implementing these steps, mention it in the comment section. Furthermore, if you have any suggestions, they will be most welcomed. Click here to know about other factors also that contribute to page speed.