• Resolved Kodeks

    (@kodeks)


    I’m wondering if there is any way to set the order of merged JS-files. Right now jQuery is loaded after another script which requires jQuery.

    Is there also any way to set the script to load asynchronously?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author launchinteractive

    (@launchinteractive)

    Hi, MMR lets WordPress handle the ordering of the includes.. but you can modify it by dequeuing and enqueuing in your functions file. There is also some other plugins that allow you to re-order your scripts.

    Plugin Author launchinteractive

    (@launchinteractive)

    Also, adding async or defer can be handled using this code:

    function add_async_attribute($tag, $handle) {
        if ( !in_array($handle, array('js-0','js-1','jquery')) ) {
            return $tag;
        }
        return str_replace( ' src', ' async="async" src', $tag );
    }
    add_filter('script_loader_tag', 'add_async_attribute', 10, 2);

    Note: You will need to modify the handles to match your site.. MMR creates handles like this: js-[0 BASED POSITION]

    Also, if you want jquery to load before your script you should add jquery as a dependancy in wp_enqueue_script/register_script

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘JS ordering/async’ is closed to new replies.