How to include code only on specific pages?

WP v3.3 gave us the ability to run wp_enqueue_script in the middle of a page. Ticket 9346

This has made it much easier to include your JS with better granularity (when using shortcodes, at least). Here, jquery will be only included when our shortcode is fired.

function get_slideshow() {
  // Do some stuff...

  // Load up scripts right from within our shortcode function (requires WP 3.3+)
  wp_enqueue_script( 'jquery', array(), null, true  ); 

  return;
}
add_shortcode( 'cool_slideshow', 'get_slideshow' );

Leave a Comment