Advertisement
Guest User

Untitled

a guest
Nov 25th, 2015
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. // enqueue google web font; e.g. get_webfont('Open Sans', 'normal');
  2. function get_webfont($name, $weight='normal') {
  3. static $nums = array(
  4. 'thin' => 100,
  5. 'extra-light' => 200,
  6. 'light' => 300,
  7. 'normal' => 400,
  8. 'regular' => 400,
  9. 'medium' => 500,
  10. 'semi-bold' => 600,
  11. 'bold' => 700,
  12. 'extra-bold' => 800,
  13. 'ultra-bold' => 900
  14. );
  15.  
  16. // abort if invalid name
  17. $name = trim($name);
  18. if(empty($name)) { return; }
  19.  
  20. // convert comma-separated into array
  21. if(!is_array($weight)) {
  22. $weights = explode(',', $weight);
  23.  
  24. // pass in weight array
  25. } else {
  26. $weights = $weight;
  27. }
  28.  
  29. // abort if no weights
  30. if(empty($weights)) { return; }
  31.  
  32. // convert name to url-encoded and slug forms
  33. $url = urlencode($name);
  34. $slug = 'font-'.str_replace(' ', '-', strtolower(trim($name)));
  35.  
  36. // enqueue weights (each on a separate line to appease Internet Explorer)
  37. $ie = detect_ie();
  38. if(!empty($ie) && ($ie <= 8)) {
  39. foreach($weights as $weight) {
  40.  
  41. // convert weight to lowercase string
  42. $weight = strtolower(trim($weight));
  43.  
  44. // split weight name from extra info
  45. list($weight, $extra) = preg_split('/\s+/', $weight, 2);
  46.  
  47. // lookup weight name
  48. if(!is_numeric($weight)) {
  49. $weight = (isset($nums[$weight]) ? $nums[$weight] : 0);
  50. }
  51.  
  52. // skip if invalid weight
  53. if(!is_numeric($weight) || ($weight < 100)) {
  54. continue;
  55. }
  56.  
  57. // register & enqueue font style
  58. wp_register_style($slug.'-'.$weight.$extra, '//fonts.googleapis.com/css?family='.$url.':'.$weight.$extra);
  59. wp_enqueue_style($slug.'-'.$weight.$extra);
  60. }
  61.  
  62. // enqueue fonts
  63. } else {
  64.  
  65. // numbers list
  66. $list = array();
  67.  
  68. // convert weights to numbers
  69. foreach($weights as $weight) {
  70.  
  71. // convert weight to lowercase string
  72. $weight = strtolower(trim($weight));
  73.  
  74. // split weight name from extra info
  75. list($weight, $extra) = preg_split('/\s+/', $weight, 2);
  76.  
  77. // lookup weight name
  78. if(!is_numeric($weight)) {
  79. $weight = (isset($nums[$weight]) ? $nums[$weight] : 0);
  80. }
  81.  
  82. // skip if invalid weight
  83. if(!is_numeric($weight) || ($weight < 100)) {
  84. continue;
  85. }
  86.  
  87. $list[] = $weight.$extra;
  88. }
  89.  
  90. // register & enqueue font style
  91. if(count($list) > 0) {
  92. $weights = implode(',', $list);
  93. wp_register_style($slug, '//fonts.googleapis.com/css?family='.$url.':'.$weights);
  94. wp_enqueue_style($slug);
  95. }
  96. }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement