/
/
Wordpress education
/
Wordpress site optimization. How to speed up your site. Cache plugins.

Wordpress site optimization. How to speed up your site. Cache plugins.

Wordpress site optimization. How to speed up your site. Cache plugins.

Wordpress optimization. Why does page load speed matter ?

From this article you will learn what affects the loading speed of a website. How to optimize Wordpress and slim down from unnecessary lines of code and plugins. How images, database for Wordpress should be optimized to make the loading speed satisfactory for the user and for the new Google Core Web Vitals algorithm.

Spis treści:

  1. Wordpress site optimization
  2. Page load speed as a ranking factor
  3. Core Web Vitals Parameters
  4. How to make an HTML, CSS template
  5. How to check page speed (loading time)
  6. How to improve page load time on wordpress
  7. Configuring the wordpress optimization plugin W3Total Cache
  8. Wordpress template implementation optimization
  9. Server optimization
  10. Ready-made wordpress templates - how to choose and whether it's worth it
  11. Optimization of photos and images

Wordpress site optimization


A website's load time is one of the first elements of a website that a user perceives. Even the best designed and functional website will be of no use if the viewer, discouraged by the long loading time, simply leaves.

It's worth taking at least a few basic steps that you can do on your own to feel the difference on speed and for websites made on Wordpress to perform much better in analytics tests.

How to optimize wordpress?

Conversion decline and website optimization

A decrease in conversions will be noted for mobile devices and will be felt indirectly on Google's organic search results if the page takes too long to load.

Web page load speed as a ranking factor


In order for a user to find our site at all, it must be ranked high enough in Google search results. Correct optimization, ensuring that the site loads and runs quickly, is also important here, since page loading speed is one of the criteria for calculating search engine rankings. Therefore, when creating and managing a site, we should pay great attention to its speed.

What affects the loading time of a website ?

Core Web Vitals Parameters

The time that elapses from typing in a page URL or clicking a link to displaying the finished page to the user can be measured by several key indicators.

They are:

  1. TTFB (Time to First Byte) - the time it takes for the browser to receive the first data from the server. The most important factor for high TTFB is the optimization of our site's code. If the PHP script of our template is complicated, has many loops and queries to the database, the time to generate the page for the user will increase significantly.

  2. FCP / LCP (First / Largest Contentful Paint) - indicators that most reflect the user's perception of page loading speed. They determine the time it takes for the first (FCP) or largest (LCP) element of our page to be displayed on the screen.

  3. TTI (Time to Interactive) - tells the time when the page becomes interactive and can be fully and uninterruptedly used. It is defined as the time from FCP and the end of the last task that blocked the main thread for more than 50ms.

Don't have time ? Order 'remote admin' service to help you

We provide care and administration of your website in convenient and flexible hourly packages.

More about wordpress care service

Made Wordpress template / theme /.

There are different possibilities of writing / coding / the same graphic design. You can make a wordpress template readable with a minimal amount of JavaScript and CSS code, and there may be too much of it (often in ready-made templates), which overtaxes the way the page reads and renders.

In addition to the HTML code of the site itself, an important part of the site is all the static resources that are linked. On a typical site, these include CSS style sheets, webfont files and JavaScript scripts.

Resources blocking rendering

The file types listed above are render-blocking resources. If these resources are too large, they cause the DOM tree to load longer, resulting in slow page display.

Wordpress website case studies

See Wordpress website development, implemented at our digital agency.

Wordpress technical optimization

In this article, we will focus on the technical side of doing template coding for Wordpress.

Important. Before the planned work, it is a good idea to make a backup of the site and wordpress in advance.


How to test site speed

Before we start optimizing and trying to speed up the site, we need to identify the factors that have the greatest impact on its slow loading. When analyzing a completed site or a site that needs to be optimized, the first thing to do is to check it with analytics programs to see how the HTML coding was done and the Wordpress implementation itself.


Google PageSpeed Insights

The most important, for SEO reasons, website testing tool. It shows both the aggregate score for mobile and desktop devices, as well as individual component parameters, along with information on which ones need improvement. The results of the google tool have a direct impact on SEO ranking, so it makes sense to focus on the highest possible score.

GTmetrix

It shows key tips that will make it easier to eliminate the biggest factors contributing to a slow loading site. It also allows you to analyze the so-called waterfall, which is a graph of how individual resources load over time, which can be helpful in finding those that may be taking too long to load.

KeyCDN Performance Test

It allows us to evaluate the DNS speed and TTFB value from different locations around the world, which can be useful, especially when we target our site to audiences in different countries.

Browser console.

Allows you to accurately analyze the order and loading speed of all resources requested by the site, along with a breakdown of the components of these times. It also allows you to quickly catch oversized image files, scripts and styles, as well as emulate different Internet speeds (such as 3G or slow LTE). The Performance tab also displays all Web Vitals indicators with information on whether their values are normal or too high. For advanced users, there are also features such as "Coverage," which allows you to find unused CSS and JavaScript code.

Improving the speed of Wordpress pages

Optimization from within Wordpress

If our only task is to manage the content itself from wordpress, we can affect the page load time in the following ways:


  1. Avoiding the use of a large number of front-end plug-ins, mostly each of which loads its own CSS and JS resources.
    If we need to put a photo gallery in a post or embed a YouTube video, let's use Wordpress' built-in tools instead of reflexively looking for plug-ins in the repository.

  2. Checking the size and weight of uploaded photos - Before uploading an image to a media library, it should be run through a JPEG compression tool. Using lossy compression with a reasonable quality threshold, we are able to reduce the file weight by up to 10 times without losing much in quality.

    Also, let's not upload a page of images straight from the camera at 10MP resolution. Most templates permanently specify the width of the content container, usually in the 1200-1400px range, so there is no need to upload images with a larger width.

    A lifesaver for already existing media libraries can be optimization plugins that scan and compress images uploaded to the site. However, these plugins in their free versions very often have limitations such as the maximum number of images compressed at once, the maximum size of an image that can be processed (e.g. 1MB) or no lossy compression.

  3. Pagination of blog, comments and content - if our site displays blog articles or allows comments, it may be a good idea to limit the number of articles and comments displayed simultaneously to a reasonable number. After all, each such element to additional content, and often images that need to be uploaded.

    Wordpress also allows you to split the content of a page and an entry by using the <!--nextpage--> tag. Such a procedure can be helpful if our article contains a lot of heavy multimedia.

  4. Enabling the cache plugin - By default, when a user visits a wordpress-based site, it is generated on the fly every time using PHP scripts and database queries. Installing a cache plugin allows you to generate static HTML pages and display them to the user directly, which significantly reduces TTFB.

    However, this method will work best when our site is relatively static and doesn't contain much changing content. If, for example, we are running a store using woocommerce, then using a cache will not only be impractical (having to rebuild the cache every time prices or stock updates are made), but downright impossible for logged-in customers (after all, each of them has their own order list or account details page).

  5. Plug-ins for pooling and minification of resources - if our server does not use HTTP/2, it can be of great benefit to configure a plug-in that will combine all loaded CSS and JS files. In this way we can save a lot of time, significantly reducing the number of requests to the server.

    However, we should remember to thoroughly test the site and check the messages in the console after each change in the configuration of such a plug-in, as errors can often occur when combining and compressing several JS scripts and loading them at once. Usually they can be eliminated by adding the problematic files to the ignore list - then they will not be included in the process of merging files.

    All treatments that interfere with Wordpress loaded CSS / JS resources only work with files loaded using the built-in wp_enqueue_script and wp_enqueue_style functions - we should never load these files directly in the HTML code of our site!

  6. Resignation of iframe embedding - Each embedded iframe in the content causes a completely separate web page to load.


W3 Total Cache plugin configuration

That is, wordpress optimization with a plugin.

What is the best plugin for wordpress optimization?

One of the most popular plugins used for caching and broadly optimizing Wordpress is W3 Total Cache. W3C speeds up your site by generating static files instead of dynamically created content fetched from the database, which dramatically reduces page load time.

Due to the number of features it offers, its first configuration can be problematic for new users. The most important settings to configure are described below:


General tab allows you to quickly enable and disable modules configured in other tabs.

  1. Page Cache - Generates static HTML files for our site. If there is no frequently changed / dynamically generated content on the site (such as scheduled promotions on woocommerce store) we definitely enable this option.
  2. Minify - allows you to minify your JS / CSS code by removing newline characters, comments, etc. We should definitely enable this option, but it's worth testing the site later, and changing Minify methods if there are problems. If we plan to use Cloudflare, for example, which also has this feature, leave it disabled.
  3. Opcode Cache - option to store pre-compiled PHP scripts in cache to make their execution much faster. If only the appropriate modules are installed on our server, it is worth enabling this option
  4. Database Cache - Enabling this option on shared servers, where server speed, not database speed, is usually the limiting factor, can slow down a site. This option is best left disabled
  5. Object Cache -Like Database Cache, enabling its option on shared hosting usually causes the site to slow down. Disable
  6. Browser Cache - Tells the browser to use its own cache when serving the site. We definitely enable the option.
  7. CDN - Let's enable the option only if we use the services of an external CDN. In that case, it is worth consulting how to integrate this tool.
  8. Reverse Proxy - We leave disabled, as it requires a dedicated server.
  9. User Experience - several options:
    Lazy Load Images - enable if not using dedicated solutions in the template
    Disable Emoji - enable if we will not use emoticons
    Disable wp-embed script - enable if you do not plan to embed posts from other wordpress blogs
    Disable jquery-migrate on the front-end - enable, but it is worthwhile to test the operation of the site later and check for errors in the console
  10. Fragment Cache - The option is useful when you have dynamically generated sections on your site that you would not like to cache. However, implementing it requires proper template code customization, and the option itself is only available in the premium version of the plugin.
  11. The remaining options are left in their default state

Bookmark Page Cache - Here we can change advanced settings and conditions for the page cache:

  1. General - In most cases it is worth leaving the default options.
  2. Cache Preload - option, which generates fresh cache versions at regular intervals. It is worth enabling it to make sure that the displayed version of the page is not outdated.
  3. Purge Policy - allows you to set rules for clearing cache after publishing entries and updating page content. The default options are sufficient in most cases.
  4. The remaining options are left in their default state

  1. Bookmark Minify - allows you to change the minification settings of HTML, CSS and JS files
  1. General - leave the default
  2. HTML & XML - Enabling the first option should be enough. We can also enable inline embedding for CSS styles and JS scripts, but it's a good idea to check the performance of the page afterwards
  3. JS - We enable the main option, and then test the site. If there are problems with its performance, we can disable combine, or change the settings in the Minify engine settings section. If our server has support for HTTP/2, we enable the HTTP/2 push option
  4. CSS - Let's enable the main option. If our server has support for HTTP/2, we enable the HTTP/2 push option.
  5. The remaining options are left in their default state

Bookmark Browser Cache

  1. General:
    Set Last-Modified header: enabled
    Set expires header: enabled
    Set cache control header: enabled
    Set entity tag (ETag): enabled
    Set W3 Total Cache header: enabled
    Enable HTTP (gzip) compression: enabled
    Enable HTTP (brotli) compression: enabled
    Prevent caching of objects after settings change: disabled
    Remove query strings from static resources: enabled
    Don't set cookies for static files: enabled
    Do not process 404 errors for static objects with WordPress: enabled
    Rewrite URL structure of objects: enabled
  2. The remaining options are left in their default state

Leave the other options on the other tabs as they are set by default.

After changing each option, it is a good idea to check the correct operation of the site in another browser, where we are not logged into wordpress. You can also wait a while and check the effect of the changed settings on the speed of the site and Google PageSpeed or GTMetrix results.

Wordpress template optimization


If you're creating your own Wordpress template from scratch (and we usually implement this solution), it's a good idea to keep in mind a few best practices to reduce page load time.

Correct loading of JS scripts


Remember that JS scripts should be loaded in the footer of the page, so as not to block the loading of the page content. We should load scripts in Wordpress using the built-in function wp_enqueue_script( string $handle, string $src = '', string[] $deps = array(), string|bool|null $ver = false, bool $in_footer = false ), for which we should specify in the last argument that the script should be loaded before the closing </body> tag.

Conditional loading of resources

When creating extended pages, it can be a good idea to divide CSS and JS files into modules corresponding to individual subpages. In conjunction with page templates, we can then use conditional statements, for example, loading the resources responsible for the contact sub-page only on it:


wp_enqueue_style(‘common’);

wp_enqueue_script(‘common’);


if( is_page_template(‘contact.php’) ){

wp_enqueue_style(‘contact’);

wp_enqueue_script(‘contact’;

}


The same principle can be used to disable unnecessary loading of styles and scripts provided by plugins. For example, if we know that the only page where contact-form-7 will be embedded is the "Contact Us" template, we can opt out of loading the resources of this plugin on other pages:


if( ! is_page_template(‘contact.php’) ){

wp_dequeue_style(‘contact-form-7’);

wp_dequeue_script(‘contact-form-7’);

}


However, we should remember to thoroughly test the site after such procedures, as it happens that selective removal of resources of a particular plugin can cause JavaScript errors.

CSS splitting and lazy-loading

Separating out the styles responsible for the sections visible immediately upon entering the page and loading them immediately, inline, using the <style> tag in the <head> section can improve the FCP / LCP metrics described in the first part of the article. Once these key elements are loaded, the rest of the styles can be loaded later, using lazy-load. This approach, however, requires a good layout of the CSS structure at the beginning of its creation, so that extracting these styles themselves is as seamless as possible.


As with CSS sheets, lazy-loading can result in significant savings when it comes to first page load time. When using this method, images will be recharged as the user scrolls through the page, not taking up time and resources as soon as the site opens. However, remember not to use it for banners placed at the top of the site, as any delay in their loading will worsen FCP / LCP times.


For loading images added by the administrator in the content of Wordpress posts, we can use the following script, which will automatically set the attributes under the lazy-load plugin:


// Lazy Load

function prepare_lazyload_src($attr) {

 $attr['data-src'] = $attr['src'];

 $attr['src'] = '';

 $attr['class'] .= ' lazyload';

 return $attr;

}

add_filter( 'wp_get_attachment_image_attributes', 'prepare_lazyload_src');

Server optimization (hosting)

Server close to the target recipient / CDN

If we know that the main recipients of our site will be visiting it, for example, from Poland, it is worthwhile when choosing a server also to be guided by its physical location. The distance between the computer from which the queries are sent and the server serving them does not have a big impact on the speed of data transfer, but the large number of hops through which the packets must pass can cause delays perceptible by the user.


In the event that we don't have the option of choosing a server with a convenient location or we want the site to run quickly regardless of where it is loaded from, we can consider using a so-called CDN (content delivery network). Such systems consist of an extensive network of servers scattered around the world, which store copies of static files loaded by our site. When a request is made to a server, the CDN system sends back the requested resources from the point closest to the user, significantly reducing latency.

HTTP/2 protocol

Until now, in the HTTP 1.1 protocol, each requested resource was loaded using a separate request to the server. This caused a significant part of the time needed to load the page to be used just for handling these requests, and the files themselves could not be downloaded simultaneously.


This approach changed with the introduction of the HTTP/2 standard, in which the browser establishes only one connection to the server - this is maintained until the page is closed, and resources are loaded simultaneously, usually with a single request.


prokół http2 i przyśpieszenie zasobów strony
http/2 protocol and acceleration of resource loading

source: https://blog.cloudflare.com/http-2-for-web-developers/


If our provider allows it, we should definitely enable HTTP/2 support on the server. In this case, we should also abandon plug-ins that combine CSS sheets and JS scripts into aggregate files, because such an operation not only will no longer bring any benefit, but may even do harm - in the case of a minor change in, for example, a CSS sheet, the browser will only download for itself a new version of the changed file, and load the rest from the cache. If the files were merged, we would have to reload all the styles required by the site from scratch.

Browser cache support

Regardless of the protocol used on our server, it is a good idea to implement proper caching support in the browser. This will result in the browser not having to fetch resources from the server when visiting the site again, but will load them from its own local cache. For example, for the Apache server, you can add the following directives in the .htaccess file:


<IfModule mod_expires.c>

 ExpiresActive On


# Images

 ExpiresByType image/jpeg "access plus 1 year"

 ExpiresByType image/gif "access plus 1 year"

 ExpiresByType image/png "access plus 1 year"

 ExpiresByType image/webp "access plus 1 year"

 ExpiresByType image/svg+xml "access plus 1 year"

 ExpiresByType image/x-icon "access plus 1 year"


 # Video

 ExpiresByType video/webm "access plus 1 year"

 ExpiresByType video/mp4 "access plus 1 year"

 ExpiresByType video/mpeg "access plus 1 year"


 # Fonts

 ExpiresByType font/ttf "access plus 1 year"

 ExpiresByType font/otf "access plus 1 year"

 ExpiresByType font/woff "access plus 1 year"

 ExpiresByType font/woff2 "access plus 1 year"

 ExpiresByType application/font-woff "access plus 1 year"


 # CSS, JavaScript

 ExpiresByType text/css "access plus 1 month"

 ExpiresByType text/javascript "access plus 1 month"

 ExpiresByType application/javascript "access plus 1 month"


 # Others

 ExpiresByType application/pdf "access plus 1 month"

 ExpiresByType image/vnd.microsoft.icon "access plus 1 year"

</IfModule>


In case there is a need to force an update of some file, you can always use an additional parameter specifying its version.


An example of using such parameters for loading script, style and image might look like the following:


wp_enqueue_script('main', get_template_directory_uri() . '/js/app.js', array('jquery'), filemtime( get_template_directory() . '/js/app.js' ), true);

// automatic version update based on file modification date


wp_enqueue_style('main', get_template_directory_uri() . '/css/main.css', array(), filemtime( get_template_directory() . '/css/main.css' ), 'all');

// automatic version update based on file modification date


<img src=”<?php echo get_template_directory_uri();?>/assets/img/logo.png?v=2.0”>


How to speed up a website ?

There are many methods to improve the loading speed and performance of wordpress pages, and we should tailor each of them to our specific case, type of site and server configuration. However, speed is one of the most important criteria for how a site is perceived by both the user and SEO algorithms. Let's remember to regularly test the health of our site and perform wordpress database optimization work.

Wordpress ready-made templates

If we want the user to open our site efficiently and navigate through it, it is worth taking care to choose solutions that will not put too much load on the server and browser. See how to design a site on Wordpress.

Templates for every purpose (multipurpose)

Instead of choosing a template with a multipurpose option, i.e. one that gives you the ability to configure multiple layouts and layouts for the main page and subpages, let's choose a simpler theme that doesn't give you that choice, and thus doesn't come with too much JS and CSS code.

Wordpress - off-the-shelf optimization

It is often difficult or not as feasible as a dedicated implementation made under specific design and functional assumptions. Usually ready-made solutions are based on builders such as Divi, Elementor. They provide great opportunities, but at the expense of the quality of the code, which must be prepared for all possibilities, hence there is often simply too much of it, and the actual amount used is 30-40%.

Speed optimization and the number of database queries and code overloaded with various types of shortcodes loading the relevant elements into sections is sometimes so large that it is technically impossible to optimize this solution.

Plug-ins and licenses

Often plug-ins used in a ready-made Wordpress template do not have active licenses, making it impossible to update them. Failure to update often leads after time to the site being hacked or becoming infected.

No logic in wordpress panel

The way the functions and operations are implemented may be less intuitive or cumbersome to actively run the site.

See also WordPress - what it is. Guide and description of the panel

Wordpress image and photo optimization

That is, how to take and optimize photos in Wordpress ? How Wordpress handles image handling mechanisms and how to prepare images for Wordpress.

Media library

WordPress comes with one collection / directory / for managing files and graphics, which is accessible from the side menu under the name 'media'. This is where all media files like images, videos, mp3s, pdfs are stored. The library allows you to manage, edit or delete files from all over wordpress.

It also allows you to edit the alt description and title of the photo, and the 'Edit Image' button allows you to perform basic simple transformations like cropping, rotating, mirroring or scaling the original image. This type of photo editing can be done from within the added photo to the post by right-clicking on it and selecting the edit option from the pop-up menu.

Sizes from images generated by wordpress

WordPress automatically converts uploaded images to particular defined sizes. Therefore, if you upload large photos, there is no need for additional formatting into smaller sizes (if you do not want dedicated graphics under resolutions such as: 480px)

Wordpress has several basic formats for generating images from a base photo. These sizes are generated during automatic photo processing and depend on your media library settings. The variant of the photo that will be used in the post depends on the option selected when adding the photo to the post, which can of course be changed when editing the photo.

Image formats in wordpress - thumbnail, medium, large

The sizes of each version of the photo can be adjusted in the "Media Settings" tab. More advanced users can also make their own sizes by creating new markings using the add_image_size() function;

Image formats jpg and png

The proper format of the photo on the website is important. The available and popular formats are jpg and png. However, each has its own use and is subject to optimization.

PNG format

Used in graphics as a non-background. Ideal for presenting content where it is difficult to establish a uniform background.

JPEG format

Basic format for photos and classic graphics that can be further compressed reducing the weight

If you do not know whether to embed a given graphic in png and or jpg format, decide on the format that ultimately has less weight.

Webp format

A format developed by Google to further reduce the weight of an image while maintaining maximum quality. In version 5.8, Wordpress also just supports the WebP format.

In case of uploading photos to wordpress library above the weight of 1-2mb, it is a good idea to shrink them beforehand to, for example, 1920px width as they will be automatically reformatted to the set wordpress dimensions. The uploaded photo will only take up space on the server.

Compress images before adding to wordpress

When adding photos of high weight but already in the target size to an entry or, for example, to a banner, it is worth considering compressing them in advance with external programs such as: compressor.io giving great results when compressing jpg formats but also png.

Lazy-load images and photos

That is, dynamically retargeting images as you scroll the page.

Lazy-load functionality for images causes them to load dynamically. The whole process is designed to speed up page loading without having to load all the graphics across the page below the first scroll. This greatly optimizes the resources needed to show the page to the user and gives the page better performance in analytics tools.