I have a script I'd like to include with Wordpress. This script relies on another (prettyPhoto) to run, as well as on JQuery being loaded. I'd like my script to be the very last script to be included on the page.

After reading about using wp_enqueue_scipt in this answer: https://stackoverflow.com/a/19914138/1745715

And after fostertime was nice enough to walk me through a little debugging down in the comments below, I've found that I can't enqueue my script, as the last script to load (themify.gallery.js, I was mistaken in the comments below when I thought it was carousel.js) is done so asyncronously in a function inside a Themify javascript file called main.js (themes/themify-ultra/themify/js/main.js)

Here's where it's loaded:

    InitGallery: function ($el, $args) {
        var lightboxConditions = ((themifyScript.lightbox.lightboxContentImages && $(themifyScript.lightbox.contentImagesAreas).length && themifyScript.lightbox.lightboxGalleryOn) || themifyScript.lightbox.lightboxGalleryOn) ? true : false;
        if ($('.module.module-gallery').length > 0 || $('.module.module-image').length > 0 || $('.lightbox').length || lightboxConditions) {
            if ($('.module.module-gallery').length > 0)
            this.LoadAsync(themify_vars.url + '/js/themify.gallery.js', function () {
                Themify.GalleryCallBack($el, $args);
            }, null, null, function () {
                return ('undefined' !== typeof ThemifyGallery);

It would stand to reason that I could simply add my javascript include right here within this function after the line that loads themify.gallery.js. However, I want to ensure that I can upgrade my themify theme without causing the change to be lost. To solve this problem I'm using a child theme. As this main.js is located within the themes folder, is it possible to overwrite it the same way I would a normal template file, and make my change? Even if it is, is it really safe to overwrite the entire main.js file when I'm concerned about my ability to upgrade Themify later down the road? Or Is there a less destructive way to inject my include code into this function from elsewhere? Open to suggestions on the best solution.

2 Answers 2


After doing some reading, I found that it is not possible to overwrite one function in a JS file in that way. The solution was to overwrite the entire main.js file in order to make the change. I then enqueued my new main.js script with the exact same name that it was registered to in the original:

// Enqueue main js that will load others needed js
wp_register_script('themify-main-script', '/wp-includes/js/main.js', array('jquery'), THEMIFY_VERSION, true);

I would have simply commented on your post first, but I don't have enough reputation... :/

Are all the other scripts loaded via wp_enqueue_script?

If you var_dump( $GLOBALS['wp_scripts']->registered ); you can see all registered/enqueued scripts (via wp_enqueue_script : how to change loading order of scripts?)

You should be able to change the dependency ( array('jquery') ) handle in your wp_register_script to array('handle-of-last-enqueued-script') this will make sure your script isn't loaded until that one is.

  • fostertime, that all makese perfect sense. So I checked the list, found the very last file to be loaded is carousel.js (from the Themify theme that's in use), found the handle it's registered under 'themify-carousel-js'. So I replaced array('jquery') with array('themify-carousel-js'). Sadly, after dumping the cache and refreshing a few times, I find my script still exactly where it was before, smack dab in the middle of the other JS files being loaded. Certainly not at the end of the list after carousel.js where I'd expect it. Any ideas?
    – carbide20
    Mar 8, 2016 at 1:23
  • My bad, there are two separate carousel.js files loaded, and my cardflip is indeed after one of them, just not the right one. Will report back and let you know if I get it in the right place. Thanks!
    – carbide20
    Mar 8, 2016 at 1:42
  • Adding some more info to my question now after further digging.
    – carbide20
    Mar 8, 2016 at 4:02

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.