custom layout HTML
-
Hi Frank,
is there a way to generate cleaned HTML output line by line instead of one line code?
Happy new year!
-
well, you could disable the stock HTML optimization and do your own thing using the new autoptimize_html_after_minify filter to alter the HTML yourself?
happy new year to you too!
frankcould you please give me a sample w/ what I have to change to add breaks for HTML optimizing?
I also need to keep the <![CDATA]]> tags in source.Regards
RalphWell, taking into account that HTML optimization typically removes whitespace, linebreaks and comments, what would you want to do? just remove whitespace?
yep, whitespace removal but HTML should be visable line by line currently the html optimizer shows a one line source …
would it be possible?hmmm, something like
add_filter('autoptimize_html_after_minify','cluster_html_minify',10,1); function cluster_html_minify($in) { $out=preg_replace('/^\s+|\s+$/m', '', $in); return $out; }If you disable HTML optimization, this should remove whitespace at the beginning or end of each line.
Do be ware though; this is not tested at all, but just an simple example based on the regular expression found in this stackoverflow answer.
hope this helps,
frankthank you, looks very good 🙂
if I disable the HTML from backend I have to use that in /theme/functions.php right?## edit ##
the minified inline css do not line break at the end</style><title>123456</titke>
any way to add a break only after </style>
Ralph
in functions.php is OK, but if not in a child theme you’ll loose these changes when a theme update gets applied.
regarding the line break after </style>; you could do a str_replace on $out, replacing “</style>” with “</style>\r\n”?
frank
thanks Frank, it seems to work – except the </script> part for $out3, did not add the break …
I’m doing something wrong here?add_filter(‘autoptimize_html_after_minify’,’cluster_html_minify’,10,1);
function cluster_html_minify($in) {$out0 = preg_replace(‘/^\s+|\s+$/m’, ”, $in);
$out1 = str_replace(“</style>”, “</style>\r\n”, $out0);
$out2 = str_replace(“</title>”, “</title>\r\n”, $out1);
$out3 = str_replace(“</script>”, “</script>\r\n”, $out2);
$output = str_replace($out0, $out1, $out2, $out3);return $output;
}well, this line
$output = str_replace($out0, $out1, $out2, $out3);doesn’t really make sense. remove it and do
return $out3instead of
return $outputfrank
thank you, it looks much better now 🙂
a last question, can Autoptimize move java script snippets added by a plugin into the footer too?
fex:<script type=”application/ld+json”>
</script>
and
<script type=”text/javascript”>
</script>ld+json is intentionally left in place. is it not real javascript and will not impact performance (and aggregating it broke some stuff badly).
text/javascript-snippets will by default be aggregated & minified, except if you exclude those in your config off course.
frank
OK, I understand, but there is one script left in header which is not moved into the footer:
<script type=”text/javascript”>
var bhittani_plugin_kksr_js = {“nonce”:”1e5464eae6″,”grs”:true,”ajaxurl”:”https:\/\/www.mydomain.tld\/wp-admin\/admin-ajax.php”,”func”:”kksr_ajax”,”msg”:”Rate this post”,”fuelspeed”:400,”thankyou”:”Thank you for your vote”,”error_msg”:”An error occurred”,”tooltip”:”0″,”tooltips”:[{“tip”:””,”color”:”#ffffff”},{“tip”:””,”color”:”#ffffff”},{“tip”:””,”color”:”#ffffff”},{“tip”:””,”color”:”#ffffff”},{“tip”:””,”color”:”#ffffff”}]};
</script>ah, I see; scripts that have “nonce” in them (or post_id, for example) are excluded by default, as those will explode your cache (as the nonce can change with every page load).
frank
ugh, currently they are placed between my hreflang tags …
is there any way to move them in last order before </head> ?
Regards, Ralphafraid not (yet, anyway).
frank
The topic ‘custom layout HTML’ is closed to new replies.