Add inline script

If you’re enqueuing a script that requires additional Javascript to run properly (ex: targeting the proper div for your carousel and adding parameters), add that as an inline script rather than creating a separate JS file.

In the example below, I’m seeing if the post has a gallery. If it does, I load the slick carousel script (registered in functions.php), then add an inline script that runs slick on my .product-gallery.

$gallery = ea_cf( 'ea_product_gallery' );
if( $gallery ) {
  
  wp_enqueue_script( 'slick' );
  wp_add_inline_script( 'slick', 
  'jQuery(document).ready(function($){
    $(".product-gallery").slick({
        autoplay: false, 
        arrows: false, 
        dots: true, 
    });
  });' );
  
  // output gallery
}

Bill Erickson

Bill Erickson is the co-founder and lead developer at CultivateWP, a WordPress agency focusing on high performance sites for web publishers.

About Me
Ready to upgrade your website?

I build custom WordPress websites that look great and are easy to manage.

Let's Talk