Bundled theme: Add preconnect to fonts.gstatic.com in 2012-15 themes.

Add preconnect hinting for `https://fonts.gstatic.com` in the bundled themes using Google fonts. WordPress versions 4.7+ include a crossorigin attribute, earlier versions will not.

Props leobaiano, swissspidy, peterwilsoncc.
Fixes #37171.


git-svn-id: https://develop.svn.wordpress.org/trunk@38870 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Peter Wilson 2016-10-23 01:55:49 +00:00
parent d792f370ef
commit 3f14125b2b
4 changed files with 100 additions and 0 deletions

View File

@ -285,6 +285,31 @@ function twentyfifteen_scripts() {
} }
add_action( 'wp_enqueue_scripts', 'twentyfifteen_scripts' ); add_action( 'wp_enqueue_scripts', 'twentyfifteen_scripts' );
/**
* 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 );
/** /**
* Add featured image as background image to post navigation elements. * Add featured image as background image to post navigation elements.
* *

View File

@ -277,6 +277,31 @@ function twentyfourteen_admin_fonts() {
} }
add_action( 'admin_print_scripts-appearance_page_custom-header', 'twentyfourteen_admin_fonts' ); add_action( 'admin_print_scripts-appearance_page_custom-header', 'twentyfourteen_admin_fonts' );
/**
* Add preconnect for Google Fonts.
*
* @since Twenty Fourteen 1.9
*
* @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 twentyfourteen_resource_hints( $urls, $relation_type ) {
if ( wp_style_is( 'twentyfourteen-lato', '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', 'twentyfourteen_resource_hints', 10, 2 );
if ( ! function_exists( 'twentyfourteen_the_attached_image' ) ) : if ( ! function_exists( 'twentyfourteen_the_attached_image' ) ) :
/** /**
* Print the attached image with a link to the next attached image. * Print the attached image with a link to the next attached image.

View File

@ -190,6 +190,31 @@ function twentythirteen_scripts_styles() {
} }
add_action( 'wp_enqueue_scripts', 'twentythirteen_scripts_styles' ); add_action( 'wp_enqueue_scripts', 'twentythirteen_scripts_styles' );
/**
* Add preconnect for Google Fonts.
*
* @since Twenty Thirteen 2.1
*
* @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 twentythirteen_resource_hints( $urls, $relation_type ) {
if ( wp_style_is( 'twentythirteen-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', 'twentythirteen_resource_hints', 10, 2 );
/** /**
* Filter the page title. * Filter the page title.
* *

View File

@ -157,6 +157,31 @@ function twentytwelve_scripts_styles() {
} }
add_action( 'wp_enqueue_scripts', 'twentytwelve_scripts_styles' ); add_action( 'wp_enqueue_scripts', 'twentytwelve_scripts_styles' );
/**
* Add preconnect for Google Fonts.
*
* @since Twenty Twelve 2.2
*
* @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 twentytwelve_resource_hints( $urls, $relation_type ) {
if ( wp_style_is( 'twentytwelve-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', 'twentytwelve_resource_hints', 10, 2 );
/** /**
* Filter TinyMCE CSS path to include Google Fonts. * Filter TinyMCE CSS path to include Google Fonts.
* *