Last active
March 3, 2025 15:17
-
-
Save davemac/cca3fa1ec2dc55efc9c642846ef61335 to your computer and use it in GitHub Desktop.
WordPress enqueue google font with preconnect
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
wp_enqueue_style( 'dmc-google-font-fragment', 'https://fonts.googleapis.com/css2?family=Montserrat:wght@300&family=Roboto:ital,wght@0,500;1,400&display=swap', array(), null ); | |
function dmc_google_font_loader_tag_filter( $html, $handle ) { | |
if ( $handle === 'dmc-google-font-fragment' ) { | |
$rel_preconnect = "rel='stylesheet preconnect'"; | |
return str_replace( | |
"rel='stylesheet'", | |
$rel_preconnect, | |
$html | |
); | |
} | |
return $html; | |
} | |
add_filter( 'style_loader_tag', 'dmc_google_font_loader_tag_filter', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment