Separate head and body scripts #57

szepeviktor opened this Issue Aug 8, 2016 · 3 comments


None yet
3 participants

szepeviktor commented Aug 8, 2016

Is there an option to have aggregated head scripts and aggregated body scripts separately?


futtta commented Aug 8, 2016

no, there is not (yet).

pbowyer commented May 2, 2017

Thanks for your work on Autoptimize. I switched to it last month; previously I was using BWP-Minify, which had highly variable load times and broke Google's "This Site is Mobile Friendly" check.

Due to legacy code the site I'm working on needs some JavaScript in the <head>, so I used WordPress's wp_enqueue_script to assign some scripts to the head and the majority to the footer. BWP Minify compressed these where they were, and I ended up with two minified JS files.

I understand why Autoptimize scans the HTML vs hooks into the WordPress functions. I thought property $domovelast would let me specify which files to put in <body> when "Force JavaScript in <head>?" is checked, but that's not its purpose.

Are you open to a patch that would add this functionality? I envisage another setting called "Preserve head/body location of JavaScript", and then changing the parsing and extraction to treat the <head> and <body> separately, and then merge back together or not depending on the setting.

If there's a better way to handle this, or it is already present, it'd be great to hear.


futtta commented May 2, 2017

Welcome @pbowyer :-)

Best way to do this without having to code would be not to force JS in head, but to exclude the JS you want to stay in head (which I assume/ hope is a short list). you could then use the new autoptimize_filter_js_minify_excluded filter in the upcoming AO2.2 to minify the JS in head?

But if you were to code and you would do a PR, I would most certainly consider off course :-)

@futtta futtta added the enhancement label Sep 27, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment