Possible Duplicate:
wp_enqueue_script isn't connecting my custom js file

I am really confused as to how wp_enqueue_script works. I have the following code in my header, and when I inspect it in safari in the resources tab I can see it, however in the source code for the html there is nothing linking to it. So I don't understand how it shows up in the resources tab without a file linking to it. Also how would I link multiple js files using this method. I have been stuck at this all day.

add_action('template_redirect', 'add_my_script');

function add_my_script() {
        plugins_url('my-script.js', __FILE__),
  • 1
    When you say you have the code in your header do you mean header.php? If so template_redirect has already fired. You should be hooking into wp_enqueue_scripts instead of template_redirect. – Chris_O Feb 21 '12 at 5:13
  • Or put your code in functions.php – chrisguitarguy Feb 21 '12 at 5:23
  • to be honest I don't exactly understand this to much. If I place this is my functions.php file that js file be rendered on the page? – Anders Kitson Feb 21 '12 at 5:27
  • Actually...if you are in the theme, you shouldn't use plugins_url. That will lead to the wrong URL. What is the path to "my-script.js"? Are you doing this as part of a theme or a plugin? – tollmanz Feb 21 '12 at 5:32
  • 1
    HONESTLY: I really tried to help you in the other Q yesterday. You mark the answer as solution to your problem and then open the exact same Q again? Why don't you just comment and try to update your Q until the problem is solved? – kaiser Feb 21 '12 at 12:34

Your code is correct, but you will not see this <script> tag in your header!

The last parameter of wp_enqueue_script() is a flag regarding whether or not to load the script at the bottom of the page. You have this set to true, so you're telling WordPress to create a <script> tag at the bottom of your HTML source, not in the <head> element.

Also, as mentioned in the comments, you should not be using the template_redirect hook for this. Use add_action( 'wp_enqueue_scripts', 'add_my_script' ); instead.

  • Please see the comments on the OP. This question is an exact duplicate. – Chip Bennett Feb 21 '12 at 15:28

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