A bulletproof way to avoid jquery conflicts in wordpress plugins

I have been developing plugins for wordpress for a long time, and it always seems to me that the following problems arise with all my conflicts of jQuery plugins.

I tried so many different ways to avoid this, but I always force users to contact me, saying when they installed one of my plugins, it stopped the other plugin from working aahhhhh.

I really want this sorted out because I understand how it can be frustrating for people.

I always installed and added or added jQuery wordpresses, below is an example of non-working code.

add_action( 'init', array( $this, 'include_jquery' ) ); function include_jquery(){ wp_deregister_script('jquery'); wp_register_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"), false, '1.7.1'); wp_enqueue_script('jquery'); } 

Ok, so after problems with this, I now have a select option in the admin plugin to switch yes or no to enable jquery or not, I know that it is automatically installed, but some users uninstall it, it works for some people, but not for all.

if you enable jQuery Wordpress, I know that you need to run jquery with the following.

 jQuery(document).ready(function ($) { 

jQuery instead of dollar sign $

I understand and used jquery no conflict, and tried and tested some if not all http://api.jquery.com/jQuery.noConflict/

 $.noConflict(); jQuery(document).ready(function($) { // Code that uses jQuery $ can follow here. }); 

This is like for others, for some, but not all users, with conflicts that still occur with certain users.

I hope that from this publication, some of the wordpress plugin developers could help and publish a bulletproof way to use wordpress and jquery in our plugins without any conflict issues.

thanks

+7
source share
2 answers

Does this work with closure?

 (function($){ // your plugin code })(jQuery); 
+7
source

Read these pieces of code:

Instead of init you should use wp_enqueue_scripts hook.

And you should use jQuery.noConflict(); instead of $.noConflict(); .

+1
source

All Articles