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: https://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'); 

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x