How to add jQuery?
WordPress plugins and themes are part of what makes the CMS so popular, as they allow the customization and optimization of the websites hosted on the platform.
However, to take full advantage of these elements, it is important to know how to add and use jQuery scripts.
But do you know what jQuery is? It is a JavaScript library created by John Resig in 2006. Its main purpose is to associate with JavaScript elements in HTML in order to provide more dynamism and usability to web pages.
Its lines of code simplify the scripts that are interpreted by the client-side browser and for this reason, it is one of the most popular libraries in the developer community. Donβt know how to use this feature?
Donβt worry. In this text, we will describe the main steps for you to add jQuery scripts to WordPress.
We will go over:
Check jQuery compatibility;
Entering jQuery stealth mode;
Entering no conflict mode;
Enqueuing jQuery in WordPress.
Keep reading and learn more!
Check jQuery compatibility
Since WordPress is an open-source tool that deals with different types of plugins and themes, the system uses a mechanism to ensure compatibility between different applications.
Therefore, it is necessary to make sure that jQuery is added in a way that does not interfere or conflict with other libraries.
To do this, all you need is a small change in the way you write your lines of code. To make jQuery compatible, you should not use the $ sign in your code, as you probably do in other projects. Instead, you should use the jQuery command.
So, look at the example below:
As it uses the $ sign directly, this line of code could encounter compatibility problems. To avoid this, it should look like this:
Simple, isnβt it? Just replace the dollar sign with jQuery to make the code compatible with WordPress. There is only one problem: this substitution can make the code too long and hard to read. However, we have some good news.
You can change the settings to use the $ sign without any problems. For that, there are two options: using the stealth mode or the no conflict mode. We will talk more about them in the next topics.
Entering jQuery stealth mode
If you are new to jQuery, you must understand how the script structure works before you learn to enter stealth mode. The framework looks like this: $(selector).action().
Letβs break it down for you:
$ β the dollar symbol communicates that it is a jQuery script;
(selector) β acts as query, representing an HTML element that needs to be found;
action() β defines the function that will be executed on the element.
Now, as we saw in the last topic, WordPress requires you to use the compatibility mode, replacing the dollar sign. To get around this and enter stealth mode, you need to enter a code that maps jQuery to $. Using an anonymous function, wrap the following code:
Here, it is important to note that this process only works in the footer. If you are wondering, it is feasible to run something similar in the header, although this may slow down your website. Therefore, it is not a recommended action.
However, if you need to, you should wrap the whole code in a document-ready function, looking like this:
Entering no conflict mode
Another way to avoid the need to spell jQuery every time you apply the script is by entering no conflict mode. This allows you to replace the dollar sign with the j$ alias but you can define a different symbol later.
To enter conflict mode, follow the example below:
Last updated
Was this helpful?