Last active
October 7, 2020 23:38
-
-
Save cameronjonesweb/ede74039eee9f67caccfa007dad02e19 to your computer and use it in GitHub Desktop.
[PHP] Helper function for retrieving Google Fonts stylesheet URLs
This file contains 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
<?php | |
/** | |
* Generates a URL for getting a Google Font stylesheet URL | |
* | |
* @copyright 2017 Cameron Jones | |
* @license MIT | |
* @param array $args | |
* @return string URL of the Google Font family | |
*/ | |
function cameronjonesweb_google_fonts( $args ) { | |
$return = false; | |
if( !empty( $args ) && is_array( $args ) ) { | |
$return = 'https://fonts.googleapis.com/css?family='; | |
$i = 0; | |
foreach( $args as $key => $val ) { | |
// If the key is numeric there are no weights and val is actually the family | |
if( is_numeric( $key ) ) { | |
$family = $val; | |
$weights = array(); | |
} else { | |
$family = $key; | |
$weights = $val; | |
} | |
$return .= str_replace( ' ', '+', $family ); | |
if( !empty( $weights ) && is_array( $weights ) ) { | |
$return .= ':'; | |
for( $j = 0; $j < count( $weights ); $j++ ) { | |
$return .= $weights[$j]; | |
if( $j+1 < count( $weights ) ) { | |
$return .= ','; | |
} | |
} | |
} | |
if( $i+1 < count( $args ) ) { | |
// We have multiple families so add a seperator. | |
$return .= '|'; | |
} | |
$i++; | |
} | |
} | |
return $return; | |
} |
This file contains 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
<?php | |
/** | |
* Get a single font with no weights. | |
* Google Fonts will just return a stylesheet for the 'normal' font weight if no weights are provided. | |
* @returns https://fonts.googleapis.com/css?family=Open+Sans | |
*/ | |
$fonts = cameronjonesweb_google_fonts( array( | |
'Open Sans' | |
) ); | |
/** | |
* Get a single font with weights. | |
* Note weights must be an array. | |
* @returns https://fonts.googleapis.com/css?family=Open+Sans:300,400 | |
*/ | |
$fonts = cameronjonesweb_google_fonts( array( | |
'Open Sans' => array( 300, 400 ) | |
) ); | |
/** | |
* Get multiple fonts with no weights. | |
* Google Fonts will just return a stylesheet for the 'normal' font weight if no weights are provided. | |
* @returns https://fonts.googleapis.com/css?family=Open+Sans|Roboto | |
*/ | |
$fonts = cameronjonesweb_google_fonts( array( | |
'Open Sans', 'Roboto' | |
) ); | |
/** | |
* Get multiple fonts with weights. | |
* Note weights must be an array. | |
* @returns https://fonts.googleapis.com/css?family=Open+Sans:300,400|Roboto:300,400,700 | |
*/ | |
$fonts = cameronjonesweb_google_fonts( array( | |
'Open Sans' => array( 300, 400 ), | |
'Roboto' => array( 300, 400, 700 ) | |
) ); | |
/** | |
* Get multiple fonts, some with weights and some without. | |
* Note weights must be an array. | |
* Google Fonts will just return a stylesheet for the 'normal' font weight if no weights are provided. | |
* @returns https://fonts.googleapis.com/css?family=Open+Sans:300,400|Lato|Roboto:300,400,700 | |
*/ | |
$fonts = cameronjonesweb_google_fonts( array( | |
'Open Sans' => array( 300, 400 ), | |
'Lato', | |
'Roboto' => array( 300, 400, 700 ) | |
) ); | |
/** | |
* Enqueue a stylesheet in WordPress | |
*/ | |
function enqueue_google_fonts() { | |
wp_enqueue_style( | |
'google-fonts', | |
cameronjonesweb_google_fonts( array( | |
'Open Sans' => array( 300, 400 ), | |
'Lato', | |
'Roboto' => array( 300, 400, 700 ) | |
) ) | |
); | |
} | |
add_action( 'wp_enqueue_scripts', 'enqueue_google_fonts' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment