Opened 5 years ago
Closed 5 years ago
#47282 closed enhancement (fixed)
Use font-display for Google Fonts in core themes
Reported by: | westonruter | Owned by: | westonruter |
---|---|---|---|
Milestone: | 5.3 | Priority: | normal |
Severity: | normal | Version: | |
Component: | Bundled Theme | Keywords: | has-patch |
Focuses: | Cc: |
Description
Google Fonts just landed a long-requested ability to specify the font-display
property for the @font-face
rules they serve. This allows fonts to be served with font-display:swap
which prevents FOIT (Flash Of Invisible Text) on slow connections. Given that the core themes have fallback fonts in place for when the Google Font is not available, it seems they should certainly opt for swap
or fallback
. For more info, see `font-display` for the Masses and avoiding invisible text during font loading.
Attachments (3)
Change History (12)
@
5 years ago
Also use display=swap for Open Sans Δ https://github.com/westonruter/wordpress-develop/pull/1/commits/25fddd967940a8b6ffbaa10af9920e1e4d798e67
#1
@
5 years ago
- Milestone changed from Awaiting Review to 5.3
- Owner set to flixos90
- Status changed from new to reviewing
Makes sense, this is an easy win we can do for 5.3. Looping in @laurelfulford on this.
#2
@
5 years ago
I lean towards using fallback
instead of swap
for better UX. See https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face/font-display#Values for differences.
@
5 years ago
Switch from swap to fallback Δ https://github.com/westonruter/wordpress-develop/pull/1/commits/5aeb0f5852c78a75844f3dd32de609da181694ef
#3
@
5 years ago
Some themes don't have the function as pluggable. Maybe you could make them so that it can be changed in the child theme easily?
#4
@
5 years ago
@superpoincare as long as the stylesheets are enqueued properly, then you can just amend the URL of the registered stylesheet. For example:
<?php add_action( 'wp_enqueue_scripts', function() { $handle = 'my-google-font-stylesheet'; if ( ! wp_style_is( $handle, 'registered' ) ) { return; } wp_styles()->registered[ $handle ]->src = add_query_arg( 'display', 'fallback', wp_styles()->registered[ $handle ]->src ); }, 100 );
#5
@
5 years ago
@westonruter
True.
Actually a simpler solution is to use the filter style_loader_src and use str_replace. Or just use wp_enqueue_style with the preferred url, since it doesn't override and the child functions.php loads first.
But I was just aiming for giving more flexibility for users.
Anyway there are enough flexibilities already, so it's not necessary to make the function pluggable.
#7
@
5 years ago
- Resolution fixed deleted
- Status changed from closed to reopened
This seems working for all themes except Twenty Twelve where it's giving a 404.
Reopening.
#8
@
5 years ago
- Owner changed from flixos90 to westonruter
- Status changed from reopened to assigned
Good catch. The +
is getting double-encoded. I'll commit this patch (as I need to dust off my commit!):
-
src/wp-content/themes/twentytwelve/functions.php
a b function twentytwelve_get_font_url() { 165 165 } 166 166 167 167 $query_args = array( 168 'family' => urlencode( 'Open +Sans:400italic,700italic,400,700' ),168 'family' => urlencode( 'Open Sans:400italic,700italic,400,700' ), 169 169 'subset' => urlencode( $subsets ), 170 170 'display' => urlencode( 'fallback' ), 171 171 );
Use font-display:swap for Google Fonts in core themes: https://github.com/westonruter/wordpress-develop/pull/1