Support » Plugin: Autoptimize » Loading external JS after combined Autoptimize JS

  • Resolved Václav Greif

    (@vasikgreif)


    Hello,
    I need to load external JS script, that I cannot control. The script has jQuery as dependency.

    I use Autoptimize to combine all JS files on the site and move them to the footer to make them non-renderblocking, including jQuery.

    Is there any way to load the external script after the Autoptimize combined JS, so that jQuery is already loaded and the external script does not throw “jQuery undefined” error?

    Thanks!

Viewing 1 replies (of 1 total)
  • Plugin Author Optimizing Matters

    (@optimizingmatters)

    you’ll have to change the place the AO’d JS is injected. you can do so using this a code snippet, e.g.

    add_filter('autoptimize_filter_js_replacetag','override_js_replacetag',10,1);
    function override_js_replacetag($replacetag) {
            return array("<injectjs />","replace");
            }

    the <injectjs /> would have to be added to your child theme. you can also use an existing tag to inject before or after it, e.g. array(“</head>”,”before”) or array(“</title>”,”after”).

    the easy solution though would be to exclude js/jquery/jquery.js from being optimized, in that case it is loaded earlier and will be available for the external script?

    hope this helps,
    frank

Viewing 1 replies (of 1 total)
  • The topic ‘Loading external JS after combined Autoptimize JS’ is closed to new replies.