How it works...

WordPress sites can be slowed down due to many independent reasons. In this recipe, we looked at three ways through which we can fix slow WordPress sites. We started by enabling caching on the WordPress site. In this case, we used the W3 Total Cache plugin for enabling the cache. This is a fully-featured plugin that provides different types of cache-storing mechanisms, along with the ability to configure caching for different parts of the site. In this case, we enabled the Page cache and Database cache settings to illustrate the caching features.

Once enabled, the plugin will cache the pages and database results in the disk as cached files. These files are stored in the wp-content/cache folder of the WordPress installation. Once you view a page, it will be cached inside this folder by creating a folder with the page name and storing the page content as an HTML file. Once the user requests the same page again, the cached HTML file will be loaded instead of generating the content again from the database. Therefore, the speed of loading the page decreases, as it loads a static file instead of querying and loading content dynamically.

Then, we looked at the process of minifying script and style files. Minification removes all unnecessary code from script or style files without changing the functionality. The removed code includes spaces, newlines, and comments. Once minification is done, all the formatting will be removed, and we will see the entire file as a single block of code, as follows:

!function(d,l){"use strict";var e=!1,o=!1;if(l.querySelector)if(d.addEventListener)e=!0;if(d.wp=d.wp||{},!d.wp.receiveEmbedMessage)if(d.wp.receiveEmbedMessage=function(e){var t=e.data;if(t)if(t.secret||t.message||t.value)if(!/[^a-zA-Z0-9]/.test(t.secret)){var r,a,i,s,n,o=l.querySelectorAll('iframe[data-secret="'+t.secret+'"]')

As you can see, no spaces, tabs, or newlines in the code making it hard to read. However, file size is reduced considerably for large files. We enabled minification using the W3 Total Cache plugin and cleared the cache. Now, all the JavaScript and CSS files will be minified automatically. These files include the scripts and styles from the theme and plugins. You can check this by viewing the source code and clicking one of the script files of a plugin before enabling minification. Then, check the same file after minification. Now, you won't see the file in the source code with the original filename. Instead, the dynamic file will be created on the wp-content/cache/minify/ folder with the minified content of the original file. After minification, your site will speed up, depending on how much of the size has been reduced through minification.

Next, we used the Query Monitor plugin to check the queries executed on any given URL. We can see all the queries and filter them to see if the query is generated from WordPress core, a plugin, or a theme. We can find the slowest posts/pages on the site and view the executed queries. Sometimes, we have plugins activated even when they are not used for any feature. Using the queries, we can track plugins slowing the site and see if they can be deactivated, replaced with a better plugin, or optimized to reduce queries.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.186.143