Gelform builds Web Applications

When you have to use $ instead of jQuery

Recently I had to use a third-party javascript library that uses $, not jQuery. WordPress only pushes jQuery to the front-end. Here’s how I got around it.

Add a new javascript file to your plugin, called jquery.noconflict.js (this can be called anything, as long as it’s consistent). In that file put:

var $ = jQuery.noConflict();

Enqueue this file like so:

wp_enqueue_script (
'jquery.noconflict',
plugins_url('jquery.noconflict.js', __FILE__),
array('jquery')
);

Now enqueue your third-party script, adding the script above as a dependency:

wp_enqueue_script(
'some-script',
'//some-script.com/some-script.js',
array('jquery.noconflict')
);

Now jQuery will load, it will be set to $ and then your third-party script will have access to it.

← Blog