0

I am trying to register my JavaScript file in my function.php file. But when I add in the function to load the JavaScript file, nothing happens. When I go into dev tools and inspect it on the browser, it isn't appearing there either. (I am working locally)

Here is my PHP code:

function add_scripts() {
  wp_enqueue_script('scripts', get_template_directory_uri() . 'js/scripts.js', array('jquery'), '1.0.0', true);
}
add_action('wp_enqueue_scripts', 'add_scripts');

This is at the bottom of my function.php file, and the file, scripts.js is located in the js folder in my theme folder.

I should at least be seeing this load when i inspect the webpage with my dev tools, but it's not appearing. Does anyone know what might be causing this?

1
  • Try changing the handle scripts (first parameter) to something a little less generic (eg. my_theme_name_main_script) and see if it helps. Commented May 10, 2019 at 3:12

4 Answers 4

4

You are missing / before js path folder. Add follows -

function add_scripts() {
  wp_enqueue_script('your_scripts', get_template_directory_uri() . '/js/scripts.js', array('jquery'), '1.0.0', true);
}
add_action('wp_enqueue_scripts', 'add_scripts');
Sign up to request clarification or add additional context in comments.

Comments

0
function theme_enqueue_styles() {
   wp_enqueue_script( 'script-js', get_stylesheet_directory_uri() . '/js/script.js', array( 'jquery' ), '20181207',true );
}
add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles', 12 );

This method is tested and working

Comments

0

Just missed out with the '/' in file path. Please be careful with the handle i.e script. Just copy the code and replace your one.

function add_scripts() {
  wp_enqueue_script('scripts', get_template_directory_uri() . '/js/scripts.js', array('jquery'), '1.0.0', true);
}
add_action('wp_enqueue_scripts', 'add_scripts');

Comments

0

please make sure your id is unique. first param in wp_enqueue_script.

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.