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

JavaScript

Linking JavaScript: when to use wp_register_script?

In the video we used it for flexslider.js but not for theme.js (code below). For the latter, why was it okay to just "absorb" the register's parameters into wp_enqueue_script instead of use the actual WordPress function?.

    //Load Theme's JS
function theme_js() {
    wp_register_script('flexslider', get_template_directory_uri().'/js/flexslider.js', array('jquery'), '', true);
    if(is_page('home')) {
        wp_enqueue_script('flexslider');
    }
    wp_enqueue_script('theme_js', get_template_directory_uri().'/js/theme.js', array('jquery'), '', true);
}

1 Answer

Tom Bedford
Tom Bedford
15,645 Points

I think registering a script allows you to use it later on specific pages. In the example you posted the flexslider is used on the "home" page. The theme.js is enqueued straight away as it will be used on every page.

The parameters only need to be set once. For flexslider.js they are set when it's registered, it's then called by name in the enqueue as it's already been set. For theme.js the parameters are set with the enqueue as there's no need to "register" it as it will be on every page.