Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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');
?>
share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

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()

share|improve this answer
    
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. –  Anders Kitson Mar 11 '12 at 19:26
2  
@AndersKitson that's a dependency. codex.wordpress.org/Function_Reference/wp_enqueue_script –  soulseekah Mar 11 '12 at 19:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.