I had a pretty specific use-case where I needed jQuery to load earlier than usual. After googling “how to force wordpress to load jquery in the header”, I got all kinds of advice about how to replace the WordPress version of jQuery by deregistering the default and registering a different version, etc.

I didn’t want to do that. I definitely don’t want to maintain another version, not unless it gives me features I can’t get any other way. In this case, the WordPress version was just fine; I just needed it available sooner.

One suggested I didn’t come across, possibly because it’s “too obvious,” was the head-slapping one that finally occurred to me: simply enqueue another Javascript file, make jQuery a dependency, and set the $in_footer parameter to false.

In my case, this looks like:

wp_enqueue_script( 'smartystreets', plugins_url( 'assets/smartystreets.js', __DIR__ ), array( 'jquery' ), '1.0.1', false );

Now both jQuery and this script load in the header, no additional futzing required. I would not hesitate to add a completely empty “script” if needed. Sure, it would result in an extra HTTP request, but it sure beats the “jQuery is not defined” error!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.