I'm trying to load an external javascript file that uses jquery but references it with '$'. Through some digging around, I've cobbled together the following:

    (function($) {
        $.getScript('https://cdn.timekit.io/booking-js/v2/booking.min.js', function() {
            var widget = new TimekitBooking();
                app_key: 'test_widget_key',
                project_id: '1234'
                // optional configuration here.. (read more under: configuration)
    })( jQuery );       

I've then enqueued this script in the footer. However, I keep getting the error 'TypeError: $ is undefined' in line 45 of the loaded script and lines 10 and 15 of this one.

Is this the best method of achieving what I want, and if so, what am I doing wrong?

  • Did you add jquery as a dependency for the script? e.g. wp_enqueue_script( 'your-script', 'path/to/script/file', array( 'jquery' ) ) – Sally CJ Aug 21 '19 at 14:39
  • I hadn't, but have done so now, unfortunately without success. :( – Badger Aug 21 '19 at 15:20
  • "$ is undefined' in line 45 of the loaded script" - at the top of the script, add window.$ = jQuery; and see if the issue persists. And regardless yes or not, I think the problem is in the script itself and has nothing to do with PHP - do check the page source, though - see if jQuery is actually loaded on the page. – Sally CJ Aug 21 '19 at 16:24
  • 1
    Thanks very much. window.$ = jQuery; seems to have done the trick. – Badger Aug 22 '19 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.