How to Preload Font in WordPress Functional Plugin

I use a plugin to do a bunch of optimizations on this site, but for some reason, the preloading of fonts is broken. So I rolled my own font preloading function and placed it in my functional plugin (You could also put it in functions.php). Here’s the code:

// Add code to <head>
function tmm_add_to_header(){
	?>
	<link rel="preload" href="/path/to/font.woff2" as="font" type="font/woff2" crossorigin>
	<?php
};
add_action('wp_head', 'tmm_add_to_header');

That’s it!

Thanks to web.dev for the font preload code.

Posted in

Toby Cryns

Toby Cryns is a freelance CTO, expert WordPress developer, and teacher.

He offers free advice to improve your freelance biz.

He also publishes small droppings every now and them to https://twitter.com/tobycryns and https://twitter.com/themightymo.com

Follow Toby's contributions on Github and WP.org.