jQuery Plugin Uncaught TypeError: jQuery(…).slider is not a function
-
23-04-2021 - |
题
I made a plugin for my webpage. It works as a standalone HTML file fine. But when I use it as wp plugin a jQuery Plugin I need (Bootstrap Slider https://github.com/seiyria/bootstrap-slider) throws this error above.
I included jquery and this boostrap-slider plugin like that:
wp_enqueue_script( 'child_jquery','cdn URl');
wp_enqueue_script('bootstrap-slider', 'CDN Url');
And call the plugin in my code so:
jQuery("#m1,#m2,#m3,#m4,#m5,#m6,#m7,#m8,#m9,#m10,#m11,#m12").slider({
reversed : true,
tooltip: 'show'
});
This call is within a document ready function:
jQuery(document).ready(function(){
My suspicion, after the plugin is loaded a further jquery is enqued. So in the source code I have (simplified):
jquery.js
bootstrap-slider.js
jquery.js (an old one from wp)
I tried to deregister it and right after that enqueue it again. But if I do that the plugin still doesn't work and another plugin throws this error.
Any ideas?
Thanks in advance
解决方案
You can try this code:
add_action("wp_enqueue_scripts", "my_jquery_enqueue", 11);
function my_jquery_enqueue() {
wp_deregister_script('jquery');
wp_register_script('jquery', "//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js", false, null);
wp_enqueue_script('jquery');
wp_enqueue_script('bootstrap-slider', 'CDN Url', array('jquery'), false, null);
}
What I am doing is deregistering the WP jQuery script. Then I'll add a new jQuery script with the CDN. I will enqueue this script and then enqueue your bootstrap slider script with jquery as a dependency. This means jQuery will be loaded BEFORE Bootstrap Slider.
Let me know if this works for you.
其他提示
I had a similar problem with my BxSlider Plugin which is now fixed (see excerpt below from my wcslider.php file) using your method, thank you very much.
function wcslider_scripts() {
wp_enqueue_style('bxslider', WCSLIDER_PATH . '/css/jquery.bxslider.min.css');
if(wp_script_is('jquery', 'enqueued')) {
wp_deregister_script('jquery');
wp_register_script('jquery', "//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js", false, null);
wp_enqueue_script('bxsliderjs', WCSLIDER_PATH . '/js/jquery.bxslider.min.js', array('jquery'), false, null);
} else {
wp_enqueue_script('jquery');
} wp_enqueue_script('bxsliderjs', WCSLIDER_PATH . '/js/jquery.bxslider.min.js', array('jquery'), false, null);
}
add_action('wp_enqueue_scripts', 'wcslider_scripts');