Categories
guides wordpress

Preload Resources in a WordPress theme functions.php file

Sometimes a report from Lighthouse will indicate you should preload a resource. Adding this (and replacing the href, and making sure you use the correct as= value) to your theme’s functions.php file will preload a resource.

function lx_preload() {
        ?>
        <link rel="preload" href="/wp-content/themes/Avada/includes/lib/assets/fonts/icomoon/icomoon.woff" as="font" crossorigin>
        <?php
}
add_action( 'wp_head', 'lx_preload', 5);

Note that in the case of fonts, the cross-origin attribute is required, even if the file is not actually cross-origin.

By bo.

I'm @boyEatsSteak in a lot of places. I have been wanting 🍕 for weeks. I like gadgets and technology and keeping notes and tips for my future self since all I can remember is that I forget things.