# Load a Script or Stylesheet Only on Selected Pages

> Enqueues a JavaScript or CSS file only on the pages you list, instead of on every page, to keep the rest of the site fast.

- Type: PHP
- Runs: Run everywhere
- Category: [Developer](https://wpide.com/library/category/developer/)
- Tags: scripts, styles, enqueue, performance
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/dev-load-script-on-pages/

## Code

```php
add_action( 'wp_enqueue_scripts', function () {
    $pages      = array( 'contact', 'booking' ); // Page slugs or IDs.
    $script_url = '';                            // Full URL of the JavaScript file.
    $style_url  = '';                            // Full URL of the CSS file.
    $version    = '1.0.0';                       // Change to bust browser caches.

    if ( ! is_page( $pages ) ) {
        return;
    }

    if ( $script_url ) {
        wp_enqueue_script( 'wpide-page-script', esc_url_raw( $script_url ), array(), $version, array( 'strategy' => 'defer', 'in_footer' => true ) );
    }

    if ( $style_url ) {
        wp_enqueue_style( 'wpide-page-style', esc_url_raw( $style_url ), array(), $version );
    }
} );
```

## Notes

Set $pages to page slugs or IDs and $script_url / $style_url to your files. Leave a URL empty to skip it.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Load a Script or Stylesheet Only on Selected Pages" 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/
