Despite the fact WordPress has been around for a while, and the method of adding scripts to themes and plugins has been the same for years, there is still some confusion around how exactly you’re supposed to add scripts. So let’s clear it up. Since jQuery is still the most commonly used Javascript framework, let’s take a look at how you can add a simple script to your theme or plugin.
jQuery’s Compatibility Mode
Before we start attaching scripts to WordPress, let’s look at jQuery’s compatibility mode. WordPress comes pre-packaged with a copy of jQuery, which you should use with your code. When WordPress’ jQuery is loaded, it uses compatibility mode, which is a mechanism for avoiding conflicts with other language libraries.
What this boils down to is that you can’t use the dollar sign directly as you would in other projects. When writing jQuery for WordPress you need to use jQuery instead. Take a look at the code below to see what I mean:
/* Regular jQuery */
$(‘.hideable’).on(‘click’, function() {
$(this).hide();
})
/* Compatibility Mode */
jQuery(‘.hideable’).on(‘click’, function() {
jQuery(this).hide();
})
The good news is that with
Source: https://managewp.org/articles/14686/how-to-properly-add-jquery-scripts-to-wordpress
source https://williechiu40.wordpress.com/2017/03/24/how-to-properly-add-jquery-scripts-to-wordpress/
No comments:
Post a Comment