» » » How to avoid jquery conflicts in WordPress

How to avoid jquery conflicts in WordPress

posted in: jQuery, WordPress | 0
Facebook
Google+
https://applerinquest.com/how-to-avoid-jquery-conflicts-in-wordpress">
LinkedIn
Follow by Email
1 min read

As WordPress developers know that the jQuery in WordPress runs in noConflict mode. Which 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.

 

Facebook
Google+
https://applerinquest.com/how-to-avoid-jquery-conflicts-in-wordpress">
LinkedIn
Follow by Email