Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Enqueue and preload a font in WordPress
function enqueue_font_preload() {
wp_enqueue_style('example-font-handle', '', array(), null);
add_action('wp_enqueue_scripts', 'enqueue_font_preload');
function style_loader_tag_filter_preload($html, $handle) {
if($handle === 'example-font-handle') {
$new_html = str_replace("text/css", "font/woff2", $html);
return str_replace("rel='stylesheet'", "rel='preload' as='font' crossorigin='anonymous'", $new_html);
return $html;
add_filter('style_loader_tag', 'style_loader_tag_filter_preload', 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment