How can I preload fonts on wordpress?

Nathan Bernard picture Nathan Bernard · Mar 28, 2019 · Viewed 10.2k times · Source

Running Pagespeed on a wordpress i'm working on it says 4.5sec are taken by this resource: /wp-content/themes/dt-the7/fonts/icomoon-the7-microwidgets-16x16/fonts/icomoon-the7-microwidgets-16x16.ttf?dqeoeo

and it suggests to use preload for it.

So I tried to add this to the header-single.php in wordpress before </head>:

<link rel="preload" href="/wp-content/themes/dt-the7/fonts/icomoon-the7-microwidgets-16x16/fonts/icomoon-the7-microwidgets-16x16.ttf?dqeoeo" as="font" type="font/ttf" crossorigin="anonymous">

but without any result. Does someone know how I can preload such content on a wordpress?

Answer

monirakandha picture monirakandha · Dec 31, 2020

I face same problem when trying to improve speed for mobile.Try that way but that is not working for me. then writte code for preload. this is work for me. First go to function.php in your theme...Note Must create your theme child.

add_action('wp_head' , function(){
    echo'
    <link rel="preload" herf="font Url" 
    as="font" type="font/woff2" crossorgin>
    <link rel="preload" herf=" font Url" 
    as="font" type="font/woff2" crossorgin>
    ';
});