Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // enqueue google web font; e.g. get_webfont('Open Sans', 'normal');
- function get_webfont($name, $weight='normal') {
- static $nums = array(
- 'thin' => 100,
- 'extra-light' => 200,
- 'light' => 300,
- 'normal' => 400,
- 'regular' => 400,
- 'medium' => 500,
- 'semi-bold' => 600,
- 'bold' => 700,
- 'extra-bold' => 800,
- 'ultra-bold' => 900
- );
- // abort if invalid name
- $name = trim($name);
- if(empty($name)) { return; }
- // convert comma-separated into array
- if(!is_array($weight)) {
- $weights = explode(',', $weight);
- // pass in weight array
- } else {
- $weights = $weight;
- }
- // abort if no weights
- if(empty($weights)) { return; }
- // convert name to url-encoded and slug forms
- $url = urlencode($name);
- $slug = 'font-'.str_replace(' ', '-', strtolower(trim($name)));
- // enqueue weights (each on a separate line to appease Internet Explorer)
- $ie = detect_ie();
- if(!empty($ie) && ($ie <= 8)) {
- foreach($weights as $weight) {
- // convert weight to lowercase string
- $weight = strtolower(trim($weight));
- // split weight name from extra info
- list($weight, $extra) = preg_split('/\s+/', $weight, 2);
- // lookup weight name
- if(!is_numeric($weight)) {
- $weight = (isset($nums[$weight]) ? $nums[$weight] : 0);
- }
- // skip if invalid weight
- if(!is_numeric($weight) || ($weight < 100)) {
- continue;
- }
- // register & enqueue font style
- wp_register_style($slug.'-'.$weight.$extra, '//fonts.googleapis.com/css?family='.$url.':'.$weight.$extra);
- wp_enqueue_style($slug.'-'.$weight.$extra);
- }
- // enqueue fonts
- } else {
- // numbers list
- $list = array();
- // convert weights to numbers
- foreach($weights as $weight) {
- // convert weight to lowercase string
- $weight = strtolower(trim($weight));
- // split weight name from extra info
- list($weight, $extra) = preg_split('/\s+/', $weight, 2);
- // lookup weight name
- if(!is_numeric($weight)) {
- $weight = (isset($nums[$weight]) ? $nums[$weight] : 0);
- }
- // skip if invalid weight
- if(!is_numeric($weight) || ($weight < 100)) {
- continue;
- }
- $list[] = $weight.$extra;
- }
- // register & enqueue font style
- if(count($list) > 0) {
- $weights = implode(',', $list);
- wp_register_style($slug, '//fonts.googleapis.com/css?family='.$url.':'.$weights);
- wp_enqueue_style($slug);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement