WordPress: Move inline JS to be fired after deffered jQuery WordPress: Move inline JS to be fired after deffered jQuery wordpress wordpress

WordPress: Move inline JS to be fired after deffered jQuery


I have worked very hard with this same issue. There are a few things to consider here. This problem would be easy if every plugin enqueued scripts in Wordpress properly, unfortunately, many do not and instead just echo the script or use a standard include.

As some commenters have noted if you want to catch all inline scripts you have no choice but to search the output buffer for script tags because if a plugin echoes a script there is nothing you can do in WordPress after that as it is sent to the buffer.

Keep in mind, using preg statements and searching entire html docs and then manipulating strings is probably much more expensive than letting jQuery load in the head. That will increase the server response time for the html doc and this wait time is just as bad because it is also time were no rendering is happening and the user is sitting there with a blank screen. Not to mention, moving jQuery to footer means anything that depends on it will not work until the footer is parsed. This is the same reason Google recommends putting analytics code in the head. What if a user navigates away before the analytics code had a chance to parse?

Google Page speed is a guide, not a rule. Don't bang your head against a wall trying to get a perfect score.