Skip to content

Instantly share code, notes, and snippets.

@inc2734
Created June 6, 2018 03:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inc2734/117b9af577d360667bf528c08248b827 to your computer and use it in GitHub Desktop.
Save inc2734/117b9af577d360667bf528c08248b827 to your computer and use it in GitHub Desktop.
public function _set_preload_stylesheet( $tag, $handle, $src ) {
$handles = apply_filters( 'inc2734_wp_page_speed_optimization_output_head_styles', [] );
if ( in_array( $handle, $handles ) && 0 === strpos( $src, home_url() ) ) {
$sitepath = site_url( '', 'relative' );
$abspath = untrailingslashit( ABSPATH );
if ( $sitepath ) {
$abspath = preg_replace( '|(.*?)' . preg_quote( $sitepath ) . '$|', '$1', $abspath );
}
$parse = parse_url( $src );
$buffer = \file_get_contents( $abspath . $parse['path'] );
$buffer = str_replace( 'url(../', 'url(' . dirname( $parse['path'] ) . '/../', $buffer );
$buffer = str_replace( 'url(//', 'url(/', $buffer );
// @codingStandardsIgnoreStart
?>
<style><?php echo $buffer; ?></style>
<?php
// @codingStandardsIgnoreEnd
return;
}
$handles = apply_filters( 'inc2734_wp_page_speed_optimization_preload_stylesheets', [] );
if ( in_array( $handle, $handles ) ) {
return str_replace( '\'stylesheet\'', '\'preload\' as="style"', $tag );
}
return $tag;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment