Show Text Immediately While Google Fonts Load
Adds display=swap to every Google Fonts stylesheet your theme and plugins load, so text appears right away in a fallback font instead of staying invisible while the web font downloads.
Opens WPIDE on
Code
add_filter( 'style_loader_src', function ( $src ) {
if ( ! is_string( $src ) || false === strpos( $src, 'fonts.googleapis.com' ) || false !== strpos( $src, 'display=' ) ) {
return $src;
}
return $src . ( false === strpos( $src, '?' ) ? '?' : '&' ) . 'display=swap';
}, 20 );
Notes
URLs that already set a display value are left alone.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.