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.
Opens WPIDE on
Code
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 add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.