0

I am using the following code below in my functions.php, However app.js is not found when I inspect it in the resources tab.

It is looking for it in http://localhost:8888/goodMorningMoon/javascripts/app.js?ver=3.3.1

when it should be looking in http://localhost:8888/goodMorningMoon/wp-content/themes/Good-Morning-Moon/javascripts/app.js?ver=3.3.1

How do I modify this code so it looks in the correct directory, do I need to add get_template_directory_uri(); somewhere, I have tried but to no avail.

Thanks in Advance

p.s this is located in my functions.php file, since I read from here http://smashingshare.com/2010/09/22/how-to-properly-add-javascript-files-to-your-wordpress-site/ that that is the proper way to approach including js files.

<?php
function load_scripts() {
   wp_enqueue_script('jquery');
   wp_enqueue_script('my_javascript_file', '/javascripts/app.js', array('jquery'));
}    

add_action('init', 'load_scripts');
?>

1 Answer 1

4

You need to reference your WordPress template directory when you register the script.

Change this:

wp_enqueue_script('my_javascript_file', '/javascripts/app.js', array('jquery'));

...to this:

wp_enqueue_script('my_javascript_file', get_template_directory_uri() . '/javascripts/app.js', array('jquery'));

Codex reference: get_template_directory_uri()

2
  • thanks I thought it was something like that, I just couldn't get the syntax right. Do you know why I need array('jquery') it doesn't make sense to me. Commented Mar 11, 2012 at 19:26
  • 2
    @AndersKitson that's a dependency. codex.wordpress.org/Function_Reference/wp_enqueue_script Commented Mar 11, 2012 at 19:37

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.