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.
You should always add javascript (and styles) with the WordPress function
wp_enqueue_script()
Works like this:
after setting the
$in_footer
to true, it gets enqueued in thewp_footer()
action, usually right before the ending of the body tag.so, for you:
does the trick.
Note: Not all themes do (though all themes should) call
wp_footer();
in their footer / just above the closing</body>
tag.