I am new to WordPress and am trying to preload certain css files. I understand that they are loaded into the head using wp_enqueue_style(), so I am trying to access the html generated from that to add rel="preload".
So far it looks like this
wp_enqueue_script('main-style', 'styles/main-style.css', false, null);
add_filter('script_loader_tag', 'preload_filter', 10, 2);
function preload_filter($html, $handle) {
if (strcmp($handle, 'main-style') == 0) {
$html = str_replace("rel='stylesheet'", "rel='preload' as='style' ", $html);
}
return $html;
}
Although when I add this preload_filter function my css fails to load completely, and not just for the specified stylesheet... Am I missing anything in trying to do this, or is there a simpler method? Any help would be greatly appreciated.
main-style.cssinwp_enqueue_script()it must bewp_enqueue_style()