Last active
June 8, 2018 07:48
-
-
Save wokamoto/fb1db784c68b5a081e5f9d9838d29e79 to your computer and use it in GitHub Desktop.
[WordPress]http2_push.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action('template_redirect', function(){ | |
if ( headers_sent() ) { | |
return; | |
} | |
do_action('wp_enqueue_scripts'); | |
foreach( array('style' => wp_styles(), 'script' => wp_scripts()) as $as => $wp_links ) { | |
$link = ''; | |
foreach ( $wp_links->queue as $handle ) { | |
if ( $wp_links->registered[$handle] && ! preg_match('/-ie8$/i',$handle) && 'html5' !== $handle ) { | |
$wp_link = $wp_links->registered[$handle]; | |
$src = preg_replace('#^https?://[^/]+#', '', $wp_link->src); | |
$ver = $wp_link->ver ? $wp_link->ver : $wp_links->default_version; | |
$link .= !empty($link) ? ', ' : ''; | |
$link .= " <{$src}?ver={$ver}>; rel=preload; as={$as}"; | |
//header("Link: <{$src}?ver={$ver}>; rel=preload; as={$as}", false); | |
} | |
} | |
if ( !empty($link) ) { | |
header("Link:$link", false); | |
} | |
} | |
}, 0, 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I realized that
$wp_links->queue
doesn't have all dependencies.To get all dependencies: