From e686ac1f67fc9461fe9d01b99e527ee9a574e852 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Mon, 3 Sep 2018 00:52:53 +0000 Subject: [PATCH] Twenty Sixteen: Add pre-connect resource hint for Google Fonts. When using Google Fonts, pre-connect to https://fonts.gstatic.com to improve the performance downloading the webfont files. Props westonruter. Fixes #44668. git-svn-id: https://develop.svn.wordpress.org/trunk@43606 602fd350-edb4-49c9-b593-d223f7449a82 --- .../themes/twentysixteen/functions.php | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/wp-content/themes/twentysixteen/functions.php b/src/wp-content/themes/twentysixteen/functions.php index 676328e041..222ced2006 100644 --- a/src/wp-content/themes/twentysixteen/functions.php +++ b/src/wp-content/themes/twentysixteen/functions.php @@ -155,6 +155,27 @@ function twentysixteen_content_width() { } add_action( 'after_setup_theme', 'twentysixteen_content_width', 0 ); +/** + * Add preconnect for Google Fonts. + * + * @since Twenty Sixteen 1.6 + * + * @param array $urls URLs to print for resource hints. + * @param string $relation_type The relation type the URLs are printed. + * @return array $urls URLs to print for resource hints. + */ +function twentysixteen_resource_hints( $urls, $relation_type ) { + if ( wp_style_is( 'twentysixteen-fonts', 'queue' ) && 'preconnect' === $relation_type ) { + $urls[] = array( + 'href' => 'https://fonts.gstatic.com', + 'crossorigin', + ); + } + + return $urls; +} +add_filter( 'wp_resource_hints', 'twentysixteen_resource_hints', 10, 2 ); + /** * Registers a widget area. *