How to avoid jQuery conflicts in WordPress
Home » BLOG » WordPress » How to avoid jQuery conflicts in WordPress

How to avoid jQuery conflicts in WordPress

category:  jQuery, WordPress

WordPress developers know that jQuery in WordPress runs in noConflict mode. This means when you enqueue the script that is dependent on jQuery in WordPress, you cannot use the common $ alias. To solve this issue, you can place your code using the $ shortcut inside a noConflict wrapper.

Here is an example.

  • in PHP file
<?php
/**
 * enqueue jquery library from WordPress
 */
 wp_enqueue_script("jquery");
?>
  • in JS file
(function($) {
    
 // print out $
 console.log($);
 
})( jQuery );

If you need to load the script in the header after the document is ready, you will use the code below.

jQuery(document).ready(function( $ ) {
 
 // print out $
 console.log($);
 
});

It is an issue that some new WordPress developers don’t aware of it.