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

Getting JS files into the footer Using - wp_enqueue_script

Hey all,

I am aware (only just) of how to load an array of scripts and css files into a document in wordpress. However I want the JS files to stay at the bottom (pre closing body tag) even if they are conditional page specific scripts.

I am currently using this template = [wordpress.org/themes/boilerplate}(wordpress.org/themes/boilerplate)

How to I load them as I want to decrease loading times on my site and currently in the process of creating better cascading code with the intention of focusing on my scripts in due course.

-Neil

1 Answer

Kevin Korte
Kevin Korte
28,149 Points

Fortunately, wordpress makes this pretty easy. It is basically a 3 step proccess.

  1. Register your script in your JS function, inside of your functions.php file 2.Create a PHP if statement to check if the page is or is not this page. 3.Enqueue the script if the if statement is true.

So lets look at an example.

Let's say I have a static hfront page, and I have a slider on that page only. So I want my slider JS to only be attached in the footer on the static front. page. By default, Wordpress uses the file front-page.php for this static front page.

Keep in mind that home.php is the posts home page, different than the the static front page

So inside of my functions.php file, I might have something like this

//Load the theme javascripts
function theme_js() {
    //Register script for use on certain pages
    wp_register_script( 'slider', get_template_directory_uri() . '/js/slider.js', array('jquery'), '', true );
    //Conditionally load this JS. Check and see if we are on the front page.
        if ( is_front_page() ) {
            //If this evaluates to true, send this script
            wp_enqueue_script ( 'slider' );
        }
    //Framework script I want sent to every page
    wp_enqueue_script( 'bootstrapjs', get_template_directory_uri() . '/js/bootstrap.js', array('jquery'), '', true );
}
//Engueue our theme_js function
add_action( 'wp_enqueue_scripts', 'theme_js' );

There are lot's of ways to change is. If you wanted to send a script to to every page but the front-page.php you would check if it's NOT the front-page.php like so:

if ( !is_front_page() ) { }

Remember that the ! before a function means is not. Which means that statement says "If this is not the front-page..." and if that elevates true, it will do whatever is in the curly brackets.

To see all the different page check functions, start here, and towards the bottom of the page are all of the checks. http://codex.wordpress.org/Function_Reference/is_front_page

How do you make sure it gets sent to the footer?

Look at this http://codex.wordpress.org/Function_Reference/wp_register_script

And see that the very last variable you pass is a true or false whether you want it in the footer. The default is false so it places the script in the head. Pass it a true Boolean and it will instead place in in the footer.

And finally, in your footer.php file make sure you have <?php wp_footer(); ?> in that file, because WP will use that hook to bring in all of the enqueued scripts for the footer. Without it, no footer scripts will be brought into the page.