2

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

7

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.