How to fire a function before and after automatically in a jquery plugin?

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

Was this solution helpful to you?

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

Find solution

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.