In your theme’s functions.php:
function my_theme_slug_enqueue_script(){
if(is_page_template('path/to/template/relative/to/theme/directory/root/template_file.php')){
wp_enqueue_script( 'some_unique_string_name', 'path/to/js/file/script.js', array(), false, true );
}
}
add_action( 'wp_enqueue_scripts', 'my_theme_slug_enqueue_script' );
Change the values in my snippet to your requirements (and you need to call wp_enqueue_script()
twice, once for each of your JS files).
The third parameter allows you to tell WordPress what other scripts this script depends on (dependencies) and, if you put stuff in that array, then this script will be loaded AFTER those other scripts. The string(s) that you put into that array are the handles
of those scripts (handle
is the ‘unique name’/the first parameter in the function).
E.g. array('jquery')
.
The fourth parameter is the version number
. You can either make it false
, meaning no version number, or you can input an integer/float. This number will be appended to the URL to the asset, when loaded.
E.g. 1.0
will produce http://example.com/wp-content/themes/my-theme/js/script.js?ver=1.0
The last parameter is where to place the asset. false
to place it in the header (wherever you put wp_head()
) and true
to place it in the footer (wherever you put wp_footer()
).
You may need to change the last 2 or 3 parameters of wp_enqueue_script()
from that in my example, depending on your situation. See https://developer.wordpress.org/reference/functions/wp_enqueue_script/ for more info on wp_enqueue_script()
.