Update preloading CSS on WordPress site

A function that I have used to enable preloading CSS effectively is:

/**
 * @snippet       Update preloading CSS on WordPress site
 */
function add_rel_preload($html, $handle, $href, $media) {
if (is_admin())
    return $html;

$html = <<<EOT
<link rel='preload' as='style' onload="this.onload=null;this.rel='stylesheet'" 
id='$handle' href='$href' type='text/css' media='all' />
EOT;

return $html;
}

add_filter( 'style_loader_tag', 'add_rel_preload', 10, 4 );

Similarly you could try this for JS and Webfonts, but this has only been tested with CSS.

Thank You.

Issue preloading CSS on WordPress site, Update preloading CSS on WordPress site, Enable preloading CSS for website speed optimization, How to add an HTML tag to the header in WordPress?
About author
Kaviya Manoharan
Kaviya Manoharan
PHP and Wordpress Developer
0 comments
Be a First Comment
Add Your Comment