I am trying to enqueue a js file (calculator.js) from my theme's functions.php file.
I have a desktop theme and a smartphone theme, and I am using a plugin to switch between the two themes depending on the user's device.
I have the exact same code on my desktop theme and it works perfectly, however when I copied it to my smartphone theme, it just seems to ignore the enqueue script.
Here is my code:
function wpb_adding_scripts() {
wp_register_script('calculator_script', get_template_directory_uri() . '/js/calculator.js', array( 'jquery' ), NULL, 'all');
wp_enqueue_script('calculator_script');
wp_enqueue_style( 'slider', get_template_directory_uri() . '/css/calculator.css',false,'1.1','all');
}
add_action( 'wp_enqueue_scripts', 'wpb_adding_scripts' );
What I have done/notes:
- Made sure functions.php file does indeed start with a opening php tag
- Made sure all files are in their correct directories
- Console is not flagging any errors
- Sources does not show my js file being loaded, however the css file (calculator.css) is being loaded correctly..
Any insights as to what might be the cause of this issue?
Thank you very much.
get_template_directory_uri
will return the uri of the root / parent theme. So if you put the JS files in the child theme directory, it'll not work. To get the uri of child themes, useget_stylesheet_directory_uri()
instead.get_stylesheet_directory_uri()
orget_theme_file_uri()
.