1

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?

6
  • Do you first open page or console? Commented Mar 9, 2021 at 9:16
  • First i reload the page and then i press F12 for the console tab Commented 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 page Commented Mar 9, 2021 at 9:20
  • i did this a couple of times before, its not the problem Commented 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? Commented Mar 9, 2021 at 9:28

1 Answer 1

2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.