Im new to wordpress and im trying to load javascript to it.
This is my functions.php
add_action('wp_enque_scripts',function(){
wp_enqueue_script('$javascript', get_stylesheet_directory_uri().
'javascript/javascript.js', [], false, true);
});
and this is my javascript.js
console.log("hi");
When i open my browser's console i cant see the printed message. Plus my script file link isnt loaded in the HTML. What might be wrong? Is it that i didnt specified the javascript version?
-
Do you first open page or console?Justinas– Justinas2021年03月09日 09:16:42 +00:00Commented Mar 9, 2021 at 9:16
-
First i reload the page and then i press F12 for the console tabrahilo– rahilo2021年03月09日 09:19:17 +00:00Commented Mar 9, 2021 at 9:19
-
That's may be problem why you do not see console output. It does not write to console if it's closed. First open console, then refresh pageJustinas– Justinas2021年03月09日 09:20:30 +00:00Commented Mar 9, 2021 at 9:20
-
i did this a couple of times before, its not the problemrahilo– rahilo2021年03月09日 09:21:27 +00:00Commented Mar 9, 2021 at 9:21
-
Do you see your script file being loaded in Network tab, do you see it's link in HTML?Justinas– Justinas2021年03月09日 09:28:46 +00:00Commented Mar 9, 2021 at 9:28
1 Answer 1
Your mistake is in your action name wp_enque_scripts. You should write wp_enqueue_scripts
In addition, I think you need a slash before your folder name
add_action('wp_enqueue_scripts',function(){
wp_enqueue_script('$javascript', get_stylesheet_directory_uri().
'/javascript/javascript.js', [], false, true);
});
Sign up to request clarification or add additional context in comments.
Comments
lang-js