Call .js file from a snippet – setting the correct file path
-
HI guys,
I need to run to bits of code that would usually be placed in a child theme. The first in the functions.php file and the second is usually stored in a file in the root folder of the child theme.
What I want to check, is that I get the file path correct in the code snippet, so that it calls the file correctly. If for example I place the file in a folder called LNX in the root folder of my website, can you advise whatthe file path should be in the first part of the code below?
Or perhaps just give an example.
I have pasted the code below I am intending to use.
I want to configure this file path correctly:
‘/my-script.js’
Hopefully you can see what I mean if you look at the full script below?
Many thanks in advance for any help you can offer.
*****
The code below is designed to launch a JavaScript function after a reservation has been made. This can be used if you have conversion or tracking code for example.
In the functions.php of your child theme add:
add_action( ‘wp_enqueue_scripts’, ‘enqueue_my_js_scripts’ );
function enqueue_my_js_scripts() {
wp_enqueue_script( ‘my-script’, get_stylesheet_directory_uri() . ‘/my-script.js’, array(), null, true );
}_________
and in the child theme directory create a my-script.js file that contains:
function hbook_reservation_done() {
my_js_function();
}_________
Your Analytics code is to be added instead of my_js_function() in the given example.
Best wishes,
Lanx
- The topic ‘Call .js file from a snippet – setting the correct file path’ is closed to new replies.