I have a simple jQuery script in a WordPress plugin that is using a jQuery wrapper like this:
$(document).ready(function(){
// jQuery code is in here
}
What worked for me. The first library to import is the query library and right then call the jQuery.noConflict() method.
<head>
<script type="text/javascript" src="jquery.min.js"/>
<script>
var jq = jQuery.noConflict();
jq(document).ready(function(){
//.... your code here
});
</script>
By default when you enqueue jQuery in Wordpress you must use jQuery
, and $
is not used (this is for compatibility with other libraries).
Your solution of wrapping it in function
will work fine, or you can load jQuery some other way (but that's probably not a good idea in Wordpress).
If you must use document.ready
, you can actually pass $
into the function call:
jQuery(function ($) { ...
This solution worked for me
;(function($){
// your code
})(jQuery);
Move your code inside the closure and use $
instead of jQuery
I found the above solution in https://magento.stackexchange.com/questions/33348/uncaught-typeerror-undefined-is-not-a-function-when-using-a-jquery-plugin-in-ma
...after searching too much
<script>
var jq=jQuery.noConflict();
(function ($)
{
function nameoffunction()
{
// Set your code here!!
}
$(document).ready(readyFn);
})(jQuery);
now use jq in place of jQuery
Also, I find the good solution for use jQuery noConflict mode.
(function($){
$(document).ready(function(){
// write code here
});
// or also you can write jquery code like this
jQuery(document).ready(function(){
// write code here
});
})(jQuery);
I found this solution from here. https://thecodingstuff.com/how-to-properly-use-jquery-noconflict-mode-in-wordpress/
var $=jQuery.noConflict();
$(document).ready(function(){
// jQuery code is in here
});
Credit to Ashwani Panwar and Cyssoo answer: https://stackoverflow.com/a/29341144/3010027