Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress The WordPress Template Hierarchy Core WordPress Theme Files footer.php

How do you tell WP to load your js file within wp_footer() call instead of loading it within wp_head()?

I mean, as I understood a lot of things from functions.php are loaded through these calls and I am not sure how to be specific about this.

4 Answers

Andrew Shook
Andrew Shook
31,709 Points

You can find the documentation for the function you need here. This code should also help a little:

<?php
function YOUR_THEME_scripts() {

        wp_enqueue_script( 'handle_for_js_file', get_template_directory_uri() . '/path/to/file.js', array(/*put dependencies like jQuery in here*/), '', true/* by setting this last parameter to true, WP will ad JS to footer*/ );

        }
add_action( 'wp_enqueue_scripts', 'first_coast_scripts' );
?>

Many thanks!

m k
m k
1,907 Points

So how does the code let WP know to load something in the header versus footer?

Andrew Shook
Andrew Shook
31,709 Points

The last parameter of the wp_enqueue_scripts function will tell WP whether you want the JS included in the header or the footer. Pass in true to have it show up in the footer or pass in false/don't put anything to have it show up in the header.

m k
m k
1,907 Points

ok thanks!