Preconnect for Google Fonts in v1.7
-
Hi,
In functions.php of v1.7 of this theme, you’ll find the following:
/** * Twenty Fifteen only works in WordPress 4.1 or later. */ if ( version_compare( $GLOBALS['wp_version'], '4.1-alpha', '<' ) ) { require get_template_directory() . '/inc/back-compat.php'; }Notice that the version check above will allow WP 4.1 or newer to use the theme. Further down you’ll find the following new handler addition.
/** * Add preconnect for Google Fonts. * * @since Twenty Fifteen 1.7 * * @param array $urls URLs to print for resource hints. * @param string $relation_type The relation type the URLs are printed. * @return array URLs to print for resource hints. */ function twentyfifteen_resource_hints( $urls, $relation_type ) { if ( wp_style_is( 'twentyfifteen-fonts', 'queue' ) && 'preconnect' === $relation_type ) { if ( version_compare( $GLOBALS['wp_version'], '4.7-alpha', '>=' ) ) { $urls[] = array( 'href' => 'https://fonts.gstatic.com', 'crossorigin', ); } else { $urls[] = 'https://fonts.gstatic.com'; } } return $urls; } add_filter( 'wp_resource_hints', 'twentyfifteen_resource_hints', 10, 2 );FYI, wp_resource_hints() method is only available from WP 4.6 onwards and for some unknown reason, ‘crossorigin‘ is allowed for WP 4.7 or newer only. Anyway, I think there may be a problem for those who are using this theme on WP 4.1.x – 4.5.x
Proposed Solution 1:
Change compatible version to 4.6 but legacy versions may not be able to use this theme:/** * Twenty Fifteen only works in WordPress 4.6 or later. */ if ( version_compare( $GLOBALS['wp_version'], '4.6-alpha', '<' ) ) { require get_template_directory() . '/inc/back-compat.php'; }or
Proposed Solution 2:
Allow legacy WP versions to use this theme but do not use the preconnect if installed WP version is below 4.6:if ( version_compare( $GLOBALS['wp_version'], '4.6-alpha', '>=' ) ) : /** * Add preconnect for Google Fonts. * * @since Twenty Fifteen 1.7 * * @param array $urls URLs to print for resource hints. * @param string $relation_type The relation type the URLs are printed. * @return array URLs to print for resource hints. */ function twentyfifteen_resource_hints( $urls, $relation_type ) { if ( wp_style_is( 'twentyfifteen-fonts', 'queue' ) && 'preconnect' === $relation_type ) { if ( version_compare( $GLOBALS['wp_version'], '4.7-alpha', '>=' ) ) { $urls[] = array( 'href' => 'https://fonts.gstatic.com', 'crossorigin', ); } else { $urls[] = 'https://fonts.gstatic.com'; } } return $urls; } add_filter( 'wp_resource_hints', 'twentyfifteen_resource_hints', 10, 2 ); endif;What do you think?
The topic ‘Preconnect for Google Fonts in v1.7’ is closed to new replies.
