Is there a good practice to avoid jQuery conflicts in WordPress?
-
I got a user claiming his jQuery version is 2.0.0 and my plugin does not work. I'm fairly new to jQuery and I used simply the jQuery object to customize HTML elements like jQuery(document).ready(function($) { }); My plugin does not load any third party jQuery scripts. According to this article, http://pippinsplugins.com/why-loading-your-own-jquery-is-irresponsible/, when some themes or plugins load own jQuery, the jQuery object becomes not usable (I'm not sure) but has to use the $ object. So is there any tip to avoid plugin/theme jQuery conflicts? I'm wondering if this kind of condition helps or not. if ($.fn.pluginname == 'undefined') $.noConflict();
-
Answer:
I think you don't need an if statement. You can use: jQuery.noConflict(); jQuery(function($) { }); If the problem is conflict with libraries this will solve. In the other hand is better to use wp_enqueue_script function so Wordpress can handle the dependency for you, avoiding multiple jQuery files. Check items 1 and 4 here: http://www.ericmmartin.com/5-tips-for-using-jquery-with-wordpress/
Caio Vaccaro at Quora Visit the source
Other answers
I use this: var $j = jQuery.noConflict(); Then you can call your functions like so: $j(function() { foo } If it's not a conflict issue, make sure you are loading your plugin correctly. I suggest loading it from the functions.php file, using the wp_enque_script and noting that jQuery is a dependency. You can set this as so (lets say your plugin is called "foo.js"): function theme_js() { wp_enqueue_script( 'theme_js', get_template_directory_uri() . '/js/foo.js', array('jquery'), '', true ); } add_action( 'wp_enqueue_scripts', 'theme_js' ); That will tell WordPress that your plugin depends on jQuery.
Nicolas Ocampo
Related Q & A:
- What is a good way to structure mark-up generating code and avoid the example mess?Best solution by Code Review
- What's a good 15-25 watt starter amp that I can buy for home practice that is cheap and has good features?Best solution by Yahoo! Answers
- IS THIS A GOOD DEAL AND WILL IT BE A GOOD SYSTEM TO GET?Best solution by Yahoo! Answers
- Does anyone know a good tubing location or how to find a good tubing location near Denver?Best solution by Yahoo! Answers
- How to open a private practice?Best solution by Quora
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.