Skip to content

Instantly share code, notes, and snippets.

@RomioDiver
Forked from tdtgit/functions.php
Last active February 4, 2022 19:54
Show Gist options
  • Save RomioDiver/1beb169d7e1ffdc59cdae934080a0b5c to your computer and use it in GitHub Desktop.
Save RomioDiver/1beb169d7e1ffdc59cdae934080a0b5c to your computer and use it in GitHub Desktop.
HTTP/2 Push CSS/JS with Autoptimize WordPress's plugin
<?php
/*
* * Plugin Name: Autoptimize PUSH
* */
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