I would like to enqueue a lightbox script on the WooCommerce page content-single-product.php.

I have tried the following via functions.php:

if(is_page_template( 'page-gallery.php' ) || is_page_template( 'content-single-product.php' )) {
        wp_enqueue_script( 'theme-lightbox', get_template_directory_uri() . '/js/lightbox.js', array(), '20150323', true );
}

The script loads correctly on page-gallery.php, but not on the WooCommerce single product page. I assume this method only works for my theme page templates?

1 Answer
1

You need to delay the firing of your code, otherwise your conditionals will evaluate false (the request won’t have been parsed yet). Use the action wp_enqueue_scripts:

function wpse_182357_enqueue_scripts() {
    // Your code
}

add_action( 'wp_enqueue_scripts', 'wpse_182357_enqueue_scripts' );

Update: Missed the root of the problem – is_page_template() only works for pages. Rather than checking if the current template file is content-single-product.php, check if the request is for a single product with is_singular():

is_singular( 'product' );

Leave a Reply

Your email address will not be published. Required fields are marked *