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 a freelance WordPress developer and a contributing developer to the Genesis framework. For the past 14 years he has worked with attorneys, publishers, corporations, and non-profits, building custom websites tailored to their needs and goals.

Ready to upgrade your website?

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

Let's Talk