Skip to content

Instantly share code, notes, and snippets.

@tdtgit
Last active February 4, 2022 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save tdtgit/b0ae2200e3adfe8a54ad2b879d7c7533 to your computer and use it in GitHub Desktop.
Save tdtgit/b0ae2200e3adfe8a54ad2b879d7c7533 to your computer and use it in GitHub Desktop.
HTTP/2 Push CSS/JS with Autoptimize WordPress's plugin
<?php
add_filter('autoptimize_filter_cache_getname', 'pushAOFiles');
function pushAOFiles($in) {
$pushType = substr($in, strrpos($in, ".") + 1) === "js" ? "script" : "style";
// Do not push CSS files if it is already inline.
if ($pushType == "style" && ((bool)get_option("autoptimize_css_inline", true))) return;
header('Link: <' . $in . '>; rel=preload; as=' . $pushType, false);
return $in;
}
add_filter('autoptimize_filter_js_exclude', 'pushJQuery');
function pushJQuery($in) {
if (strpos($in, "js/jquery/jquery.js") !== false) {
$jQurl = includes_url("js/jquery/jquery.js");
header('Link: <' . $jQurl . '>; rel=preload; as=script', false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment