Last active
December 23, 2015 18:39
-
-
Save dovy/6677206 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
// 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