Hierarchical position in wp_head
-
tldr; output aggregated CSS on position 9 in
wp_head.By default, Autoptimize puts its aggregated CSS right at the top of the HTML head. I’m not sure this was always the case, but it’s not a good practice for compatibility.
The intended output order in WP is
wp_print_styleson 8 andwp_print_head_scriptson 9 inwp_head, custom CSS even later. This ensures that properly enqueued CSS files are rendered below the inline block styles, avoiding hierarchy issues that require bloated selectors or!importantto overcome.Obviously, if you now put the aggregated CSS above that, things are going to break as the block styles suddenly overrule your theme styles. Aggregating inline styles often leads to even more issues since that lacks fine control, so I do not consider that a viable solution.
Now, you can move the insertion point with the
autoptimize_filter_css_replacetagfilter to the end of the head. However, suddenly you have the opposite problem and your aggregated styles are too strong, overruling custom CSS, child themes, and plugins. Trying to filter in a replacetag in the proper position just didn’t work, I assume that it fires too late or this is not how Autoptimize works.Anyway, I believe it would be sensible to add an option or make it even default to insert the aggregated CSS where it belongs: on position 9. Otherwise this is near guaranteed to cause problems. If there is a way to do this or I did something wrong, please let me know.
You must be logged in to reply to this topic.