What are the best practices for adding font preloading in a PHP function?

When adding font preloading in a PHP function, it is important to ensure that the fonts are loaded efficiently to improve website performance. One way to achieve this is by using the "link" tag with the "preload" attribute in the HTML head section to load the fonts before they are needed. This can be done dynamically in a PHP function by generating the appropriate "link" tags for each font and echoing them out in the header of the webpage.

function preload_fonts($font_urls) {
    foreach($font_urls as $font_url) {
        echo '<link rel="preload" href="' . $font_url . '" as="font" type="font/woff2" crossorigin>' . PHP_EOL;
    }
}

$font_urls = array(
    'https://example.com/font1.woff2',
    'https://example.com/font2.woff2',
);

preload_fonts($font_urls);