ShanjayG
ShanjayG

Reputation: 1023

WooCommerce, how to load a custom javascript file while using Mystile theme?

Hi I have a WooCommerce plugin activated and a Mystile theme. I want to add a custom js file located at mystile/includes/js/custom-script.js. Ive read on how to add files through using wp_enque_script but it is not loading the js files when I view page source. I have this code block on functions.php of my current theme which is Mystile, which should have added my custom js file

function add_custom_assets(){
//Load masonry CSS and JS files
wp_enqueue_script('masonry', get_stylesheet_directory_uri().'includes/js/custom-script.js',array('jquery'), '1', true);
}

add_action('wp_enqueue_scripts','add_custom_assets');

the above code does not work and I cannot see my JS file in source. I really dont know what I am doing wrong. I even tried wp_register_scripts but its the same thing. The only change I did to the current theme was that I copied the templates folder from the woocommerce plugin directory to my current theme and renamed it woocommerce to override certain stuffs. Any suggestions

Upvotes: 1

Views: 4019

Answers (2)

funkysoul
funkysoul

Reputation: 3241

you should specify unique handles (so stuff don't get overridden)

function add_custom_assets(){
    wp_enqueue_script('my-custom-script-handle', get_stylesheet_directory_uri().'includes/js/custom-script.js',array('jquery'), '1', true);
}

add_action('wp_enqueue_scripts','add_custom_assets');

Upvotes: 2

ShanjayG
ShanjayG

Reputation: 1023

This is not a ideal solution but a hack and it does work. Enqueue the script as wp_enqueue_style rather than wp_enqueue_script. It does load the script but is a very dirty solution. Still the question is open though

Upvotes: 1

Related Questions