• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar
  • Home
  • Projects
  • Products
  • Themes
  • Tools
  • Request for Quote

Vengala Vinay

Having 12+ Years of Experience in Software Development

  • Home
  • WordPress
  • PHP
    • Codeigniter
  • Django
  • Magento
  • Selenium
  • Server
Home » Refactoring Legacy Code in Lazy Loading Assets and Critical CSS Optimizations in Multi-Language Site Networks

Refactoring Legacy Code in Lazy Loading Assets and Critical CSS Optimizations in Multi-Language Site Networks

Diagnosing and Refactoring Legacy Asset Loading

Many legacy WordPress sites, especially those with multi-language configurations, suffer from suboptimal asset loading. This often manifests as a cascade of JavaScript and CSS files being enqueued synchronously on every page load, regardless of whether they are actually used. This directly impacts perceived performance and Core Web Vitals. The first step in refactoring is accurate diagnosis.

We’ll start by identifying the culprits. Browser developer tools are indispensable here. The Network tab, filtered by “JS” and “CSS,” will reveal every asset requested. Pay close attention to the “Initiator” column to trace which script or theme function is responsible for enqueuing each file. For a more granular view, tools like Query Monitor (if still in use and not a performance bottleneck itself) or custom debugging can pinpoint the exact `wp_enqueue_script` and `wp_enqueue_style` calls.

Leveraging `wp_print_scripts` and `wp_print_styles` for Conditional Loading

A common anti-pattern is enqueuing assets directly in theme template files or within plugin activation hooks without regard for context. The WordPress hook system provides a more robust and conditional approach. Instead of direct enqueuing, we should hook into actions that fire when scripts and styles are actually needed.

Consider a scenario where a specific JavaScript plugin is only required on single post pages. The legacy approach might look like this:

/* In theme's functions.php or a plugin file */
function my_legacy_enqueue_script() {
    wp_enqueue_script( 'my-plugin-script', get_template_directory_uri() . '/js/my-plugin.js', array('jquery'), '1.0', true );
}
add_action( 'wp_enqueue_scripts', 'my_legacy_enqueue_script' );

This enqueues `my-plugin.js` on *every* page. The refactored approach uses conditional logic within the `wp_enqueue_scripts` action:

/* Refactored approach */
function my_conditional_enqueue_script() {
    // Only enqueue on single post pages
    if ( is_single() ) {
        wp_enqueue_script( 'my-plugin-script', get_template_directory_uri() . '/js/my-plugin.js', array('jquery'), '1.0', true );
    }
}
add_action( 'wp_enqueue_scripts', 'my_conditional_enqueue_script' );

For more complex scenarios, especially with multi-language sites where different scripts might be needed per language or per post type within a language, we can combine `is_single()`, `is_page()`, `is_archive()`, and language-specific checks (e.g., using WPML’s `icl_object_id` or Polylang’s `pll_current_language`).

Implementing Lazy Loading for Non-Critical Assets

Beyond conditional enqueuing, truly lazy loading non-critical assets can significantly improve initial page load times. This involves deferring the loading of scripts and images until they are actually needed, often when they enter the viewport.

JavaScript Lazy Loading Techniques

For JavaScript, the `defer` and `async` attributes are native HTML solutions. `defer` ensures scripts are executed in the order they appear in the HTML, after the DOM is parsed. `async` allows scripts to be executed as soon as they are downloaded, without regard for order. For legacy code, we often need to dynamically add these attributes.

A common pattern is to enqueue scripts normally but then use a filter to modify the outputted `