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'm following some of the other people's advice here about adding my js file before the closing body tag but it doesn't seem to work for me.

If someone would be so kind as to check this for me please?

<?php
  /*load the js file into the footer*/
  function myscript() 
  {
   if( wp_script_is( 'jquery', 'done' ) ) {
  ?>
     <script type="text/javascript" src="js/scripts.js"></script>
  <?php
  }
 }
  add_action( 'wp_footer', 'myscript' );
?>

Many thanks

EDIT:------------------------------------------------

This is the short solution.. <script language="javascript" src="<?php bloginfo('template_directory'); ?>/js/scripts.js"></script>

not the right answer though is it?

EDIT---------------------------------------------------

This is what I am trying based on the WP codex examples and your points Fischi but think I am still doing something wrong..

function my_scripts_method() {
wp_enqueue_script(
    'myscript',
    get_bloginfo('template_directory') . '/js/scripts.js',
    array( 'jquery' ), 
    '', 
    true
);
 }    

 add_action('wp_enqueue_scripts', 'my_scripts_method');

The file still doesn't load into the footer.

share|improve this question
1  
The file doesn't load - does this mean the <script> tags don't appear in the footer - or is the script is not being found?. Finally does your theme call wp_footer(); –  Stephen Harris May 4 '12 at 18:00
    
Hi Stephen. My theme is using <?php get_footer(); ?>. Let me try wp_footer(); and get back to you. Thanks! –  SixfootJames May 5 '12 at 4:32
add comment

1 Answer 1

up vote 6 down vote accepted

You should always add javascript (and styles) with the Wordpress function wp_enqueue_script()

Works like this:

wp_enqueue_script( 
 $handle // the name of your enqueued file, in your case 'myscript'
,$src    // source of your file, can be external, or for your example: get_bloginfo('template_directory') . '/js/scripts.js'
,$deps   // does your javascript depend on another javascriptfile? for example jquery? pass an array of arguments specifying all the dependencies: array( 'jquery' )
,$ver   // version number of your javascript
,$in_footer // this is what you need: true
);

after setting the $in_footer to true, it gets enqueued in the wp_footer() action, usually right before the ending of the body tag.

so, for you:

wp_enqueue_script( 'myscript', get_bloginfo('template_directory') . '/js/scripts.js', array( 'jquery' ), '', true );

does the trick.

Note: Not all themes do (though all themes should) call wp_footer(); in their footer / just above the closing </body> tag.

share|improve this answer
    
Thanks very much Fischi. See my edit above....StackExchange comments are crap at clean markup. –  SixfootJames May 4 '12 at 16:39
    
Hi Fischi. My theme is using <?php get_footer(); ?>. I've tried wp_footer(); but this didn't work, the footer did not appear at all. –  SixfootJames May 5 '12 at 9:18
    
@SixfootJames no problem. note that the difference between get_footer() and wp_footer() is that get_footer() calls the template file footer.php, while wp_footer() is an action performed by wordpress to insert every javascript and style that are defined to appear in the footer. usually wp_footer() is located in the footer.php, just before the closing of the <body> tag. –  fischi May 5 '12 at 14:05
    
as steven harris mentioned, not all themes call wp_footer(). if yours doesn't insert this before the </body> tag. –  fischi May 5 '12 at 14:07
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.