- This topic has 22 replies, 8 voices, and was last updated 3 years, 9 months ago by
Ying.
-
AuthorPosts
-
August 25, 2019 at 4:46 am #992771
Kristjan
Hello,
I am loading Google Fonts via the GeneratePress theme and would like to add font-display: swap.
The code currently looks like this:
<link rel=’stylesheet’ id=’generate-fonts-css’ href=’//fonts.googleapis.com/css?family=Lato:regular,italic,700′ type=’text/css’ media=’all’ />
Adding &display=swap into the href should be enough to enable font-display swap, as shown in the default code from Google Fonts:
<link href=”https://fonts.googleapis.com/css?family=Lato:400,400i,700&display=swap” rel=”stylesheet”>
How can I add &display=swap to the GeneratePress font code?
Thanks,
KristjanAugust 25, 2019 at 9:14 am #993058Tom
Lead DeveloperLead DeveloperHi there,
Try this:
add_filter( 'generate_google_font_display', function() { return 'swap'; } );Adding PHP: https://docs.generatepress.com/article/adding-php/
Let me know 🙂
August 25, 2019 at 9:34 am #993073Kristjan
Yes, that worked! Thanks a lot.
August 25, 2019 at 9:35 am #993074Tom
Lead DeveloperLead DeveloperYou’re welcome 🙂
August 28, 2019 at 8:20 am #995574Carlos
I have a similar problem for ‘Open Sans’ fonts.

Is there a similar solution?
August 28, 2019 at 10:23 am #995667Tom
Lead DeveloperLead DeveloperThat same solution should work for you.
August 29, 2019 at 1:29 am #996077Carlos
I add:
add_filter( ‘generate_get_all_google_fonts’, function() {
return ‘swap’;
});Clean: WP Rocket and Cloudflare cache
This should have added the option to both ‘Open Sans’ and ‘Roboto’ but….

any ideas?
Cheers
August 29, 2019 at 3:22 am #996151Kristjan
One thing I also did that worked.
Add this to the Code Snippets plugin:
add_action( 'wp_enqueue_scripts','tu_remove_google_fonts', 10 ); function tu_remove_google_fonts() { wp_dequeue_style( 'generate-fonts' ); }Then add this to Appearance -> Elements -> Add Hook -> wp_head -> Entire Site + All Users:
<link href="https://fonts.googleapis.com/css?family=Lato:400,400i,700&display=swap" rel="stylesheet">Replace with your own choice of Google Fonts straight from the Google Fonts website.
August 29, 2019 at 10:29 am #996553Tom
Lead DeveloperLead DeveloperCarlos – make sure you’re using the function here: https://generatepress.com/forums/topic/add-font-display-swap/#post-993058
The one you’re using isn’t the correct filter.
Kristjan – that will work as well, but the filter I shared above will make sure your Customizer options continue to work if you change the font family.
December 14, 2020 at 3:55 am #1579653Nathan
Hi Tom,
Reviving this thread to ask:
Is it possible to add the suggestions below to our child functions.php for Google Fonts?
In summary: a combination of asynchronously loading CSS, asynchronously loading font files, opting into FOFT, fast-fetching asynchronous CSS files, and warming up external domains makes for an experience several seconds faster than the baseline.
Number 4 below has already been addressed in this thread.
<!-- - 1. Preemptively warm up the fonts’ origin. - - 2. Initiate a high-priority, asynchronous fetch for the CSS file. Works in - most modern browsers. - - 3. Initiate a low-priority, asynchronous fetch that gets applied to the page - only after it’s arrived. Works in all browsers with JavaScript enabled. - - 4. In the unlikely event that a visitor has intentionally disabled - JavaScript, fall back to the original method. The good news is that, - although this is a render-blocking request, it can still make use of the - preconnect which makes it marginally faster than the default. --> <!-- [1] --> <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin /> <!-- [2] --> <link rel="preload" as="style" href="$CSS&display=swap" /> <!-- [3] --> <link rel="stylesheet" href="$CSS&display=swap" media="print" onload="this.media='all'" /> <!-- [4] --> <noscript> <link rel="stylesheet" href="$CSS&display=swap" /> </noscript>December 14, 2020 at 6:11 am #1579860David
StaffCustomer SupportHi there,
those links need to be hooked into the
wp_headYou can do something like this:
add_action('wp_head' , function() { ?> <!-- add yours scripts here --> <?php });Or you can use a Hook Element to do the same thing
December 14, 2020 at 10:40 am #1580444Nathan
Hi David,
Thanks for the reply!
It looks like this is working now (with my specific fonts loading):
// Remove GeneratePress Google Fonts add_action( 'wp_enqueue_scripts','tu_remove_google_fonts', 10 ); function tu_remove_google_fonts() { wp_dequeue_style( 'generate-fonts' ); } // Fast Google Fonts loading add_action( 'wp_head', 'add_google_fonts_preload', 1 ); function add_google_fonts_preload() { ?> <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin /> <link rel="preload" as="style" href="//fonts.googleapis.com/css?family=Lato%3Aregular%2C700&display=swap" /> <link rel="stylesheet" href="//fonts.googleapis.com/css?family=Lato%3Aregular%2C700&display=swap" media="print" onload="this.media='all'" /> <link rel="stylesheet" href="//fonts.googleapis.com/css?family=Lato%3Aregular%2C700&display=swap" /> <?php }December 14, 2020 at 3:53 pm #1580734David
StaffCustomer SupportGlad to hear that!
March 19, 2021 at 9:19 am #1702113Sam
Hi Guys, I am curious. I have this implemented and I can see the swap option is implemented. However, Google PSI still complains about this.
From my page source: href=’//fonts.googleapis.com/css?family=Poppins:300,regular,500,600,700&display=swap’
From GPSI: Ensure text remains visible during webfont load
Warnings: Lighthouse was unable to automatically check thefont-displayvalue for the origin https://XXXXXXXXXXXXXXXXXX.com.I am wondering if it is just a technical limitation of GPSI or if I am missing something?
March 20, 2021 at 9:01 am #1703033Tom
Lead DeveloperLead DeveloperHi Sam,
Perhaps it’s a different font they’re seeing? Feel free to open a new support topic with the URL to your site and we can take a look.
Thanks!
-
AuthorPosts
- You must be logged in to reply to this topic.