Skip to content

Instantly share code, notes, and snippets.

@wokamoto
Last active June 8, 2018 07:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wokamoto/fb1db784c68b5a081e5f9d9838d29e79 to your computer and use it in GitHub Desktop.
Save wokamoto/fb1db784c68b5a081e5f9d9838d29e79 to your computer and use it in GitHub Desktop.
[WordPress]http2_push.php
<?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);
@miya0001
Copy link

miya0001 commented Mar 10, 2018

I realized that $wp_links->queue doesn't have all dependencies.

To get all dependencies:

$queue = $wp_links->queue;
$wp_links->all_deps( $queue );
$to_do = $wp_links->to_do; // All list of items that should be loaded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment