0

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?

4

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' );
  • I'm using the _S (Underscores) theme which already uses this method, so I'm guessing it's another issue. – Sam Mar 26 '15 at 14:38
  • 1
    Change is_page_template( 'content-single-product.php' ) to is_singular( 'product' ) – TheDeadMedic Mar 26 '15 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.