WordPress has been in our lives for over 16 years, yet the method of adding scripts to themes and plugins still remains a mystery for many developers. In this article we finally put the confusion to rest.
About jQuery’s Compatibility Mode
Before we start attaching scripts to WordPress, it’s important to understand jQuery’s compatibility mode.
The problem is, writing jQuery a gazillion times takes longer, makes it harder to read, and can bloat your script.
The good news?
With a few modifications you can go back to using our lovely little dollar sign once again.
BTW, if you’re new to jQuery, the $ sign is just an alias to jQuery(), then an alias to a function.
The basic structure looks like this: $(selector).action(). The dollar sign defines jQuery… the “(selector)” queries or finds HTML elements… and finally the “jQuery action()” is the action to be performed on the elements.
Back to how we can get around our compatibility issue… here are a couple of viable options:
1.Enter jQuery Stealth Mode
The first way to get around compatibility mode is to get stealthy with your code.
For example, if you’re loading your script in the footer, you can wrap your code in an anonymous function, which will map jQuery to $.
This is used more often in admin, where you’d want to use a script on a specific page only (not the whole admin). This also saves bandwidth and processing time – which means faster loading times for your website.
At Marketing Solution Australia we strive to deliverer elegant responsive websites for your business integrated with our personal SEO Optimization package to bring your pages on the first page of Google.