1

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

2 Answers 2

2

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'); 
Sign up to request clarification or add additional context in comments.

3 Comments

How was masonry not a unique handler. Anyways that did work. Cheers
The handle is different. If you enqueue a script with the same handle it might get overriden (depending on the order)..
Your theme might be loading the original masonry scripts after it loaded your script with the same handle.. always keep the handle unique and you shouldn't get into any problems. Cheers
1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.