1

i have added this code to add jQuery

wp_enqueue_script( 'jqueryadd','http://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js', array(),'22.23',true );

But i think that this is unnecessary as i believe wordpress comes with jquery and wp_enqueue script provide option to include jquery or not.

But this a script form mdbootstrap. if i remove the code above it gives me a error is console that $ is not a function it works fine if i enqueue jquery above it.

wp_enqueue_script( 'md-boot','https://cdnjs.cloudflare.com/ajax/libs/mdbootstrap/4.5.4/js/mdb.min.js', array( 'jquery' ),'1.25',true );

I have no idea what's going wrong. please help thanks

1
  • WP already comes with jQuery, you don't need to add it, and you shouldn't add it from the CDN as that can cause problems with other plugins
    – Tom J Nowell
    Aug 1, 2018 at 14:00

1 Answer 1

2

It appears that "mdbootstrap" is built in such a way that it does not support jQuery in No Conflict mode, which is how WordPress runs it. According to this forum thread they were going to add support 2 years ago, but apparently they never did.

If you must use this library, then I think the simplest solution without modifying the library directly is to add this line next to your code that enqueues the script:

wp_add_inline_script( 'jquery-core', 'window.$ = jQuery;' );

That will put WordPress' jQuery into the $ variable so that mdbootstrap can use it. Just be aware that this could cause issues if you run any other scripts that might use the $ symbol as a global variable.

That should solve the specific error you're reporting here ("$ is not a function"), but I can't guarantee that mdbootstrap works with the version of jQuery bundled with WordPress. If it doesn't, then your only option is to load a different version, but then you have to deal with the possibility that it could cause issues with other plugins expecting the bundled version.

1
  • Thank you sir. Just confirming that mdbootstrap works with wordpress jQuery but requires your code to be added.
    – user145078
    Aug 1, 2018 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.