Question

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.

Was it helpful?

Solution

You can better try this way and let me know.

function wpb_adding_scripts() {
    wp_enqueue_style( 'slider', get_stylesheet_directory_uri() . '/css/calculator.css');
    wp_enqueue_script('jquery');
    wp_enqueue_script('calculator_script', get_stylesheet_directory_uri() . '/js/calculator.js');

} 
add_action( 'wp_enqueue_scripts', 'wpb_adding_scripts' ); 

OTHER TIPS

Just some modifications

function wpb_adding_scripts() {
    wp_enqueue_script( 'calculator_script', get_template_directory_uri() . '/js/calculator.js', array( 'jquery' ), '', true );
    wp_enqueue_style( 'slider', get_template_directory_uri() . '/css/calculator.css' );
} 
add_action( 'wp_enqueue_scripts', 'wpb_adding_scripts' ); 

use get_stylesheet_directory_uri() instead of get_template_directory_uri() if your functions.php file is in child theme.

See if this works or not ( normally this way, it does )

get_template_directory_uri will always refer to the parent theme folder for assets.

get_stylesheet_directory_uri will refer to the "current" theme folder for assets (which could be the parent or the child, depending on where it is called).

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top