loadCSS - markhowellsmead/helpers GitHub Wiki
(For use in WordPress.) Thanks, Nico.
add_filter('style_loader_tag', [ $this, 'preloadCSS' ], 999, 4);
public function preloadCSS($html, $handle, $href, $media)
{
echo PHP_EOL.'<!-- ' .$handle. ' -->'.PHP_EOL;
// if ($handle !== 'main') {
// return $html;
// }
$html = str_replace('\'', '"', $html);
$html = str_replace('rel="stylesheet"', 'rel="preload" as="style" onload="this.rel=\'stylesheet\'"', $html);
return $html . '<noscript><link rel="stylesheet" data-push-id="'.$handle.'" id="'.$handle.'" href="'.$href.'" media="'.$media.'"></noscript>'.PHP_EOL;
}