How to wordpress add javascript to page template

For many purpose and designs we use the page template in wordpress site. WordPress tutorial for, How to wordpress add javascript to page template. Some time we need to load the custom javascript to page template. Here is code for same.

How to wordpress add javascript to page template

For that we can use following code.

add_action('wp_enqueue_scripts','Load_Template_Scripts_wpapi');
function Load_Template_Scripts_wpapi(){
    if ( is_page_template('custom-page.php') ) {
        wp_enqueue_script('my-script', 'path/to/script.js');
    }
}

For loading any script or any custom code you can use above action in wordpress. Above code you need to add in functions.php file. Which you can find in your wordpress theme folder.

How to wordpress add javascript to page template
How to wordpress add javascript to page template