# Unload Plugin Assets Except on the Pages That Need Them

> Removes the scripts and styles you list from every page except the ones you choose, for plugins that load their files site-wide.

- Type: PHP
- Runs: Frontend only
- Category: [Performance](https://wpide.com/library/category/performance/)
- Tags: performance, assets, cleanup
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/unload-assets-except-on-pages/

## Code

```php
// 'script:handle' or 'style:handle' => page IDs or slugs where the file is still needed.
$wpide_assets_only_on = array(
    'style:some-gallery-style'   => array( 'gallery' ),
    'script:some-booking-script' => array( 'book-now', 42 ),
);

$wpide_unload_assets = function () use ( $wpide_assets_only_on ) {
    foreach ( $wpide_assets_only_on as $asset => $pages ) {
        list( $type, $handle ) = array_pad( explode( ':', $asset, 2 ), 2, '' );

        if ( '' === $handle || ( $pages && ( is_page( $pages ) || is_single( $pages ) ) ) ) {
            continue;
        }

        if ( 'script' === $type ) {
            wp_dequeue_script( $handle );
        } else {
            wp_dequeue_style( $handle );
        }
    }
};

add_action( 'wp_enqueue_scripts', $wpide_unload_assets, 999 );
add_action( 'wp_print_footer_scripts', $wpide_unload_assets, 1 );
```

## Notes

Find handles in the page source (id="HANDLE-css" or id="HANDLE-js") and test the pages that still need them.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Unload Plugin Assets Except on the Pages That Need Them" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
