WordPress jQuery conflict mode

If you're using JQuery in WordPress you'll hit a wall with their compatibility prevention.
The tricky thing here is that the particular copy of jQuery (shipped natively with WordPress) is in compatibility mode by default. That means that the typical $ shortcut for jQuery doesn't work, so it doesn't conflict with any other JavaScript libraries that also use the dollar sign!

If the script is being loaded in the footer (which you should be doing in the vast majority of cases) you can wrap the code in an anonymous function (technically any IIFE) where you pass in jQuery to be mapped to $:

https://gist.github.com/FreshLondon/b8eb9017ce04302d385bde3eb39826f1

If you need to load the scripts in the header, you'll probably need to use a document ready function. You can just pass in $ there:

https://gist.github.com/FreshLondon/8142d074dc86b57b057fe0da16766d31

 

Filed under: 
© 2021 - FreshLondon