WordPress Snippets at WPcustoms

Load script on custom page template

This snippet loads a custom javascript file only if a conditional tag is true. In this case the script will be enqueued only if the current page uses the page template “custom-page.php”


/**
 * Snippet Name: Load script on custom page template
 * Snippet URL: http://www.wpcustoms.net/snippets/load-script-on-custom-page-template/
 */
 function wpc_load_template_script(){
    if ( is_page_template('custom-page.php') ) {
        wp_enqueue_script('my-script', 'path/to/script.js');
    } 
}
add_action('wp_enqueue_scripts','wpc_load_template_script');