Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a custom page template where I would like to load some javascript. I suppose I could always include the javacsript in the actual file, but that seems ugly. Is there any way to identify if wordpress is loading my custom-page.php file so I can enqueue the script only on that page?

It should work dynamically, so checking page id is not an option.

share|improve this question

1 Answer 1

up vote 11 down vote accepted

You can use is_page_template to check if you template is being used and load your scripts based on that ex:

add_action('wp_enqueue_scripts','Load_Template_Scripts_wpa83855');
function Load_Template_Scripts_wpa83855(){
    if ( is_page_template('custom-page.php') ) {
        wp_enqueue_script('my-script', 'path/to/script.js');
    } 
}
share|improve this answer
    
Awesome. Thank you. –  John Jan 30 '13 at 22:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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