Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

WordPress WordPress Theme Development Working with CSS and JS in WordPress Themes How to Link to JS from functions.php File

Jeff Sil
Jeff Sil
451 Points

dependencies

How do you tell what JS files are dependent on JQuery and others?

2 Answers

Austin Whipple
Austin Whipple
29,700 Points

It's pretty hard to tell in any automated fashion. If you're working with any jQuery-based plugins, then those scripts will certainly be dependent on jQuery and must be loaded after jQuery. It's rare that two different plugins will depend on each other unless specified. As for other site-specific scripts, it's always a good idea to include them last, so you can work with objects and variables defined in all libraries and plugins.

Ultimately, the best indicator for broken dependence is if you get "____ is undefined" errors in your browser console. That error is a good indication that the browser is missing required files or loading things out of order.

Jeff Sil
Jeff Sil
451 Points

Awesome, that is really helpful. Thank you.

Lets say I were to place this in the function.php folder: ''' wp_enqueue_script( 'main_js', get_template_directory_uri() . '/js/main.js', array('jquery'), '', true );

''' ...but it wasn't dependent on jquery, would that cause an error as well or cause other problems?

Austin Whipple
Austin Whipple
29,700 Points

Nah, all you're really saying by declaring jQuery in the dependencies there is that you'd like WordPress to load main.js after jQuery. If main.js doesn't actually use anything from jQuery, it's no biggy. But you'll be set up to write your own jQuery-dependent code later on, which is good.

Jeff Sil
Jeff Sil
451 Points

Great, thank you for the explanation! Some of this stuff is confusing, glad to have help.