LW IT Solutions logo LW IT Solutions logo LW IT Solutions
« Blog Overview /WordPress Plugins & Tricks/Tutorials / Performance Optimization via functions.php: Selective Dequeuing of...
Read this article in other languages:

Performance Optimization via functions.php: Selective Dequeuing of WordPress Plugin Scripts

Performance Optimization via functions.php: Selective Dequeuing of WordPress Plugin Scripts

Management Summary

WordPress plugins can load supporting files on pages that do not use the relevant feature. Selective removal means keeping those files only where needed, such as loading form support on a page that actually contains a form. This can reduce data transfer and work for the browser, helping pages run more efficiently. The whole page matters, including navigation and the footer. Files shared by several functions cannot be removed safely just because their purpose is not obvious in the main article.

Contents
  1. Unnecessary files and page features
  2. 1. Loaded assets and their dependencies
  3. 2. Dequeue and deregister are different
  4. 3. A deliberately limited example
  5. 4. Why page-type shortcuts are insufficient
  6. 5. Functionality tests before deployment
  7. Questions and answers
  8. Sources

Unnecessary files and page features

Some WordPress plugins load CSS or JavaScript on pages without the corresponding feature. Those files can add transfers, processing or render blocking. The effect depends on the actual assets and browser cache; a large script does not automatically create a large DOM or a poor INP score. The DOM is the browser’s structured representation of page elements, such as headings, links and form fields.

Selective dequeuing removes a named asset from the loading queue for a particular request. The name, called a handle, is supplied by the registering code. The useful question is not simply “Is this a shop page?” but “Does any feature on this complete page require this asset?”

The complete page is audited before proven unnecessary handles are removed; functionality and measurements are then checked.
The complete page is audited before proven unnecessary handles are removed; functionality and measurements are then checked.

1. Loaded assets and their dependencies

A staging copy should contain the same relevant theme, plugins and templates as production. Browser Network and Console panels establish which files are requested and whether features fail. The plugin source and WordPress registrations map file URLs to handles and dependency lists. A handle is an identifier, not necessarily the file name.

The WP_Scripts and WP_Styles queues provide useful diagnostic snapshots. A queue dump is not a complete inventory of all delivered assets: dependencies can be resolved later, footer or block rendering can enqueue additional files, and already printed assets need separate consideration. Script modules also have a separate API. Temporary diagnostics belong in restricted staging logs and should be removed after the audit.

2. Dequeue and deregister are different

  • wp_dequeue_script() and wp_dequeue_style() remove the explicit queue entry. Registration remains available.
  • wp_deregister_script() and wp_deregister_style() remove the registration. Required dependents can then fail to load or work correctly.

If an asset remains a registered dependency of another queued asset, dequeuing that dependency alone may not prevent loading. That is evidence of a dependency to investigate, not a reason to force removal with deregistration. Frontend deregistration has no general protection against broken features.

Priority 100 runs after callbacks with lower priorities on the same hook. A later callback or another hook can still enqueue the asset. Removal after the HTML tag has already been printed cannot undo browser loading. The correct point follows the actual enqueue and output sequence.

3. A deliberately limited example

The following example belongs in a child theme’s functions.php or a small site-specific plugin. A parent-theme update can replace edits to the parent theme. The example affects only explicitly audited singular pages or posts; the initial empty list changes nothing. An MU plugin requires the usual PHP file wrapper and deliberate maintenance.

PHP
add_action( 'wp_enqueue_scripts', function () {
    if ( is_admin() || ! is_singular() ) {
        return;
    }

    // Empty by default. Each entry requires a complete page audit.
    // Structure: post ID => scripts and styles proven unnecessary.
    $audited_pages = array();
    $post_id = (int) get_queried_object_id();
    if ( ! isset( $audited_pages[ $post_id ] ) ) {
        return;
    }

    $assets = $audited_pages[ $post_id ];
    foreach ( $assets['scripts'] ?? array() as $handle ) {
        wp_dequeue_script( $handle );
    }
    foreach ( $assets['styles'] ?? array() as $handle ) {
        wp_dequeue_style( $handle );
    }
}, 100 );

After a complete audit, an entry could have the shape 123 => array( 'scripts' => array( 'contact-form-7' ), 'styles' => array( 'contact-form-7' ) ). The number is an illustrative post ID, not a recommendation for a real site. That entry is justified only if the complete rendered page has no form and no remaining dependency on those handles. Each translation needs its own verified ID. Archive pages stay unchanged.

The example intentionally leaves registrations intact. It does not automatically inspect templates, resolve all dependencies, remove script modules or prevent later enqueue operations. Those boundaries require source inspection and a browser check. No WooCommerce-wide blocklist is included.

4. Why page-type shortcuts are insufficient

A mini-cart in the header, a product block in an article or an account feature can need commerce assets outside the main shop, cart and checkout routes. WooCommerce changed the default loading of cart fragments in version 7.8; the classic cart widget, dependencies or explicit enqueue calls can still request the script. A blanket claim that every plugin asset loads everywhere is incorrect.

Forms can appear in a footer, widget, template, reusable content or translated page. A check for the slugs contact and support misses those locations. Checking only the current post’s shortcode also misses content added elsewhere. Contact Form 7 documents native loading controls and explicit enqueue functions; using those controls still requires a complete inventory of form locations and correct timing.

5. Functionality tests before deployment

  1. A baseline uses the same route, viewport, device and cache conditions.
  2. One reviewed rule is enabled in staging. The check confirms that the intended files disappear and required dependencies remain.
  3. Tests cover forms, validation, navigation, account pages, add-to-cart, mini-cart and checkout wherever relevant, including every language and mobile layout. Styles, keyboard interaction and console errors are also checked.
  4. The tests are repeated with logged-in and logged-out sessions and the relevant cache variants. Deployment needs a reversible change and cache invalidation; a production smoke test follows staging verification.

Transferred bytes, request counts and timings must be measured. No fixed saving, disappearance of every unused-code warning or ranking improvement follows automatically. TBT is a laboratory indicator of blocking work; INP measures interaction responsiveness and needs appropriate interaction or field data. Lower TBT does not prove better INP.

Questions and answers

Why does a dequeued file still appear?

A dependency, a later enqueue, another copy of the file or cached HTML can explain the request. The request initiator, dependency tree and callback order help identify the cause. Increasing the priority blindly does not establish correctness.

Is an empty rule list useful?

The empty list makes the example inert until site-specific evidence exists. A measured, reviewed rule is preferable to assuming that every non-shop page is asset-free. The example is a starting point for that rule, not a tested configuration for an arbitrary site.

When is the audit repeated?

Theme, plugin, form, template and translation changes can invalidate a previously correct rule. The affected page tests therefore belong alongside those changes.

Lukas Wojcik

Lukas Wojcik

Systems architect and technology enthusiast specializing in scalable tracking solutions, GMP Stack (GA4 & GTM), and robust backend architectures. Advocate for clean code and privacy-first design.

Get in touch

A question or an idea for a web project? Tell me a little about it. This form uses reCAPTCHA to protect against spam.

Write a comment

Experience with other plugins or hosting environments and questions about the setup are welcome here.

The email address is not published. Required fields are marked with an asterisk.