Loading Jquery in Wordpress

JimHalpert

Supreme Member
Joined
Jan 16, 2016
Messages
1,345
Reaction score
292
I have a Jquery script that is entered into the custom code section of my wordpress theme. As the script did not run initially, I had to load Jquery dependency through functions.php. I read about this method from Step 4 of this link: (http://www.ericmmartin.com/5-tips-for-using-jquery-with-wordpress/). While this had allowed the Jquery script to run. It affected how other parts of my website was running. For example, drop down boxes could no longer function properly. (Instead of clicking the dropdown icon once to display options and then scrolling over the option they want, users now need to click and hold their mouse over the dropdown icon to display the options.)

I was just wondering if I am loading Jquery the right way? Or what is the correct way I should be approaching this.

Thank you.

Jquery script
Code:
$(function(){
       var count = 0, $input = jQuery('.cover-buttons ul li:nth-last-child(5) a'), interval = setInterval(function() {
           if ($input.hasClass('blur')) {
               $input.removeClass('blur').addClass('focus'); ++count;
           } else {
               $input.removeClass('focus').addClass('blur');
           }
           if (count === 3) { clearInterval(interval); }
       }, 1000);
});

Functions.php script to load Jquery
Code:
function my_init() {
   if (!is_admin()) {
       wp_deregister_script('jquery');
       wp_register_script('jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js', false, '3.2.1', true);
       wp_enqueue_script('jquery');

       // load a JS file from my theme: js/theme.js
       wp_enqueue_script('my_script', get_bloginfo('template_url') . '/js/theme.js', array('jquery'), '1.0', true);
   }
}
add_action('init', 'my_init');
 
The reason why your site is possibly breaking is because you have added jquery whereas it was already loaded on your website, when that happens things break. The location of the script is important as well, if you entered your jQuery script after a script that needed jQuery to run then that function would not work and that is what probably happened when you added the second jQuery CDN link.

With the information you gave here I can guess that when you used the jQuery script in the custom code of your wordpress theme before you added the second jQuery CDN link the issue most likely was that your jQuery script was added before the jQuery library was loaded as a result your script did not function. Then when you added the second CDN link through the functions.php what you did was added the jQuery library twice and possibly in the wrong place.

If I was you I would remove the code you added in the functions.php file and instead add your initial jQuery script in an individual file i.e jqueryscript.js and then add that link in your footer.php file like so:

HTML:
<script type="text/javascript" src"----location-of-your-file----/jqueryscript.js"></script>

I hope this helps and if you have more questions or need assistance just ask we are all here to help each other.
 
Back
Top