I have a code:
add_action( 'wp_enqueue_scripts', 'include_js' );
function include_js(){
wp_enqueue_script( 'bootstrapjs', "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js");
wp_enqueue_script( 'jQueryui', "http://code.jquery.com/ui/1.11.4/jquery-ui.js");
wp_enqueue_script( 'stellar', get_template_directory_uri()."/js/jquery.stellar.min.js");
wp_register_script( 'owl-carousel', get_template_directory_uri() . '/owl/owl-carousel/owl.carousel.js', array( 'jquery' ) );
wp_enqueue_script( 'owl-carousel' );
wp_enqueue_script( 'scripts', get_template_directory_uri()."/js/script.js", array( 'owl-carousel', 'jquery' ), '1.0', true );
}
In file script.js I loaded a function from ‘owl-carousel’ and I got this error:
Uncaught TypeError: $(...).owlCarousel is not a function
enter code here
This is strange because I added array(‘owl-carousel’, ‘jquery’)in the last line on the code (which is above) so script.js should load after owl-carousel but it does not. I checked html code by pressing F12 and script.js had loaded before owl-carousel. Why? Where is my mistake?
Regards
jQuery is included with WordPress by default in no-conflict mode. In
noConflict()
mode, the global$
shortcut for jQuery is not available, so you need to use something like the following to make it available to you:Owl Carousel isn’t working because it’s using
$
as a shortcut, and$
hasn’t been assigned as a shortcut for jQuery.Another option is to “dequeue” the included jQuery, and “enqueue” your own version.
Actually, if the issue was that $ is not available as a shortcut for jQuery, you would get an error stating that it doesn’t know what $ is.
The error here means that owlCarousel wasn’t loaded or not initialized.
You should check in the console whether it displays an error while loading or running the owlCarousel script (also check in the network tab which URL was called and whether it’s the right path).