Skip to content

Instantly share code, notes, and snippets.

@dovy
Last active December 23, 2015 18:39
Show Gist options
  • Save dovy/6677206 to your computer and use it in GitHub Desktop.
Save dovy/6677206 to your computer and use it in GitHub Desktop.
<?
// Construct the proper google fonts URL from the font values
function getGoogleScript( $font ) {
$data['link'] = 'http://fonts.googleapis.com/css?family=' . str_replace( ' ', '+', $font['font-family'] );
$data['key'] = str_replace( ' ', '_', $font['font-family'] );
if ( !empty( $font['font-weight'] ) ) :
$data['link'] .= ':' . str_replace( '-', '', $font['font-weight'] );
if ( !empty( $font['font-style'] ) ) :
$data['key'] .= '-' . str_replace( '_', '', $font['font-style'] );
endif;
endif;
if ( !empty( $font['subsets'] ) ) :
$data['link'] .= '&subset=' . $font['subsets'];
$data['key'] .= '-' . str_replace( '_', '', $font['subsets'] );
endif;
return $data;
}
endif;
// Enqueue google fonts if present within the given fonts array
function wc_load_google_fonts() {
global $webcoas; // This assumes your global variable (or opt_name) is $webchaos. If it is not, do a global replace.
// The ids of the fonts that are of type typography. You must specify them here.
$fonts = array(
'body-typography',
'pre-header-typography',
'menu-typography'
);
foreach ( $fonts as $font ) {
if ( !empty($webcoas[$font]) && $webcoas[$font]['google'] == true ) {
$data = getGoogleScript( $webcoas[$font] );
wp_enqueue_style( $data['key'], $data['link'] );
}
}
}
add_action('wp_enqueue_scripts', 'wc_load_google_fonts');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment