Optimizing WordPress on a Real Project: How We Reduced the First Screen by 82.3%
At LancerLab, we often face tasks related to optimizing the performance of websites. In this article, we will share our experience working on the portal of the KNUBA Library (library.knuba.edu.ua). We reduced the size of the first screen from 3.95 MB to 700 KB, which amounted to an 82.3% improvement. Let’s go through the steps of how this was achieved.
The Initial Problem
The KNUBA Library portal suffered from several issues:
- Heavy media blocks on the first screen;
- 17 JS scripts on each page;
- Duplicate fonts, which slowed down loading;
- Slow loading of the first screen.
The initial size of the first screen was 3.95 MB, significantly exceeding optimal metrics.
Step-by-Step Optimization
First Screen Media: Audit, Recompression, and Lazy Loading
The first step was to conduct an audit of media files. We used tools such as Google PageSpeed Insights to identify the heaviest images and videos.
After the audit, we recompressed all media files using tools like TinyJPG and TinyPNG. This allowed us to significantly reduce their size without losing quality.
We also implemented lazy loading of images. For this, we used the plugin Lazy Load by WP Rocket, which allows images to be loaded only when scrolling through the page.
// Example PHP code for lazy loading images
function lazy_load_images() {
return 'loading="lazy"';
}
add_filter('wp_get_attachment_image_attributes', 'lazy_load_images');
These measures allowed us to reduce the size of the first screen from 3.95 MB to 700 KB, which amounted to an 82.3% improvement.
Conditional Disabling of WooCommerce Scripts
The portal used WooCommerce for electronic publications, but not on all pages of the site. This meant that many WooCommerce scripts and styles were loaded unnecessarily.
We implemented conditional disabling of these scripts on pages outside the catalog of electronic publications using PHP hooks wp_dequeue_script and wp_dequeue_style. Example code:
// Disabling WooCommerce scripts
function dequeue_woocommerce_scripts() {
if (!is_product_category()) {
wp_dequeue_script('wc-add-to-cart');
wp_dequeue_script('wc-cart-fragments');
wp_dequeue_style('wc-block-style'); // and so on
}
}
add_action('wp_enqueue_scripts', 'dequeue_woocommerce_scripts');
This allowed us to reduce the number of loaded JS files from 17 to 11, significantly improving performance.
Database Cleanup and Open Graph / SEO Setup
We also performed a cleanup of the database from unnecessary font families. A total of 11 font families were removed, freeing up a significant amount of space.
Additionally, we configured Open Graph and SEO meta tags to improve the site’s visibility in search engines. This included using plugins such as Yoast SEO for automatically creating optimized meta tags.
Automation via WordPress MCP and REST API
For automation processes, we used Maintenance Plugin (MCP) and REST API. The AI agent was configured to reproduce block layouts designed using Gutenberg and Getwid.
The AI agent also performed layout verification in the browser, allowing us to identify and fix errors before they appeared on the site.
Final Numbers Before/After
| Metric | Before Optimization | After Optimization |
|---|---|---|
| First Screen Size | 3.95 MB | 700 KB (-82.3%) |
| Number of JS Files per Page | 17 | 11 |
| Number of Font Families | 11 | 0 |
| First Screen Load Time | 5.2 sec | 1.8 sec (-65.4%) |
These changes significantly improved the site’s performance and user experience.
Checklist for Other WordPress Sites
- Conduct an audit of media files and recompress images;
- Implement lazy loading of images;
- Conditionally disable unnecessary scripts and styles;
- Clean up the database from unnecessary font families;
- Configure Open Graph and SEO meta tags;
- Use automation tools such as REST API.
By following these steps, you can significantly improve the performance of your WordPress site. If you have any questions or need assistance, contact us at LancerLab!
Добавить комментарий