{"id":970,"date":"2014-10-17T14:14:23","date_gmt":"2014-10-17T19:14:23","guid":{"rendered":"http:\/\/littlesvr.ca\/grumble\/?p=970"},"modified":"2014-10-17T14:14:23","modified_gmt":"2014-10-17T19:14:23","slug":"how-to-stop-using-webfonts-from-google-without-breaking-your-wordpress-theme","status":"publish","type":"post","link":"http:\/\/littlesvr.ca\/grumble\/2014\/10\/17\/how-to-stop-using-webfonts-from-google-without-breaking-your-wordpress-theme\/","title":{"rendered":"How to stop using webfonts from Google without breaking your wordpress theme"},"content":{"rendered":"<p>I finally had enough of the old theme on this blog. I would have kept it but with WordPress 4 the fonts looked even smaller than they did before. I tried to fix it but found so many problems (starting withe a default font size set to 62.5%) that decided replacing it entirely will be easier. It was.<\/p>\n<p>The nice new bootstrap-based, mobile-friendly theme installed without much trouble, except for one annoying issue. I turned on Firebug to see how long it takes to load and found more than 10 calls to google.com and gstatic.com. Not cool.<\/p>\n<p><a href=\"http:\/\/littlesvr.ca\/grumble\/wp-content\/uploads\/2014\/10\/GrumbleGoogleFonts.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-972\" src=\"http:\/\/littlesvr.ca\/grumble\/wp-content\/uploads\/2014\/10\/GrumbleGoogleFonts-300x216.png\" alt=\"GrumbleGoogleFonts\" width=\"300\" height=\"216\" srcset=\"http:\/\/littlesvr.ca\/grumble\/wp-content\/uploads\/2014\/10\/GrumbleGoogleFonts-300x216.png 300w, http:\/\/littlesvr.ca\/grumble\/wp-content\/uploads\/2014\/10\/GrumbleGoogleFonts-1024x740.png 1024w, http:\/\/littlesvr.ca\/grumble\/wp-content\/uploads\/2014\/10\/GrumbleGoogleFonts.png 1028w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Notice also how much extra time it takes to finish loading the page just because of those bloody fonts on a server I don&#8217;t control. Where are the requests coming from?<\/p>\n<pre>cd wp-content\/themes\/cara # (or whatever your theme name)\r\n\r\nincludes\/tamatebako.php: * return clean and ready to use google open sans font url\r\nincludes\/tamatebako.php:function tamatebako_google_open_sans_font_url(){\r\nincludes\/tamatebako.php:        $font_url = add_query_arg( 'family', 'Open+Sans:' . urlencode( '400,300,300italic,400italic,600,600italic,700,700italic,800,800italic' ), \"\/\/fonts.googleapis.com\/css\" );\r\nincludes\/tamatebako.php: * return clean and ready to use google merriweather font url\r\nincludes\/tamatebako.php:function tamatebako_google_merriweather_font_url(){\r\nincludes\/tamatebako.php:        $font_url = add_query_arg( 'family', urlencode( 'Merriweather:400,300italic,300,400italic,700,700italic,900,900italic' ), \"\/\/fonts.googleapis.com\/css\" );\r\nincludes\/tamatebako.php:         * @link https:\/\/developers.google.com\/fonts\/docs\/webfont_loader\r\nincludes\/tamatebako.php:         * @link http:\/\/ajax.googleapis.com\/ajax\/libs\/webfont\/1\/webfont.js\r\nincludes\/tamatebako.php:        wp_register_style( 'theme-open-sans-font', tamatebako_google_open_sans_font_url(), array(), tamatebako_theme_version(), 'all' );\r\nincludes\/tamatebako.php:        wp_register_style( 'theme-merriweather-font', tamatebako_google_merriweather_font_url(), array(), tamatebako_theme_version(), 'all' );\r\njs\/webfontloader.min.js:\r\njs\/webfontloader.js:    var sa = \"\/\/fonts.googleapis.com\/css\";\r\njs\/webfontloader.js:    $.u.w.google = function(a, b) {<\/pre>\n<p>The problem is the theme is using webfontloader to load some (presumably nice) font from another server.<\/p>\n<p>If you don&#8217;t like that (and you shouldn&#8217;t, unless you already use google analytics or other big brother system :)) here&#8217;s what you can do to avoid pinging another server for every query to yours:<\/p>\n<p>Find where the font is actually used. In my case my theme is using TAMATEBAKO 1.2.2 (tamatebako.php) which asks for two fonts from fonts.googleapis.com: Open Sans and Merriweather.<\/p>\n<p>Google says &#8220;All of the fonts are Open Source. This means that you are free to share your favorites with friends and colleagues.&#8221; Great, I will share them with myself by putting them on my server.<\/p>\n<p>I could try to decode how this works in tamatebako.php:<\/p>\n<pre>add_query_arg( 'family', 'Open+Sans:' . urlencode( '400,300,300italic,400italic,600,600italic,700,700italic,800,800italic' ), \"\/\/fonts.googleapis.com\/css\" );<\/pre>\n<p>but that would just give me an unnecessary headache. Instead I will look at the source for my blog page (or Firebug) to find:<\/p>\n<pre>http:\/\/fonts.googleapis.com\/css?family=Open+Sans%3A300italic%2C400italic%2C600italic%2C300%2C400%2C600&amp;subset=latin%2Clatin-ext&amp;ver=4.0<\/pre>\n<p>and<\/p>\n<pre>http:\/\/fonts.googleapis.com\/css?family=Merriweather%3A400%2C300italic%2C300%2C400italic%2C700%2C700italic%2C900%2C900italic&amp;ver=1.0.0<\/pre>\n<p>Ok, those are two CSS file I can download, referencing a bunch of .woff files I can also download. Clickedy click click click.. I now have:<\/p>\n<pre>$ ls -lh webfonts\/\r\ntotal 388K\r\n-rw-r--r-- 1 andrew andrew\u00a0 33K Oct 16 15:49 DXI1ORHCpsQm3Vp6mXoaTRa1RVmPjeKy21_GQJaLlJI.woff\r\n-rw-r--r-- 1 andrew andrew\u00a0 22K Oct 16 15:53 EYh7Vl4ywhowqULgRdYwIFrTzzUNIOd7dbe75kBQ0MM.woff\r\n-rw-r--r-- 1 andrew andrew\u00a0 22K Oct 16 15:53 EYh7Vl4ywhowqULgRdYwIG0Xvi9kvVpeKmlONF1xhUs.woff\r\n-rw-r--r-- 1 andrew andrew\u00a0 24K Oct 16 15:53 EYh7Vl4ywhowqULgRdYwIL0qgHI2SEqiJszC-CVc3gY.woff\r\n-rw-r--r-- 1 andrew andrew\u00a0 34K Oct 16 15:49 MTP_ySUJH_bn48VBG8sNSha1RVmPjeKy21_GQJaLlJI.woff\r\n-rw-r--r-- 1 andrew andrew\u00a0 32K Oct 16 15:50 PRmiXeptR36kaC0GEAetxmWeb5PoA5ztb49yLyUzH1A.woff\r\n-rw-r--r-- 1 andrew andrew\u00a0 32K Oct 16 15:49 PRmiXeptR36kaC0GEAetxrsuoFAk0leveMLeqYtnfAY.woff\r\n-rw-r--r-- 1 andrew andrew\u00a0 17K Oct 16 15:52 RFda8w1V0eDZheqfcyQ4EHhCUOGz7vYGh680lGh-uXM.woff\r\n-rw-r--r-- 1 andrew andrew\u00a0 22K Oct 16 15:53 So5lHxHT37p2SS4-t60SlHpumDtkw9GHrrDfd7ZnWpU.woff\r\n-rw-r--r-- 1 andrew andrew\u00a0 17K Oct 16 15:53 ZvcMqxEwPfh2qDWBPxn6ngi3Hume1-TKjJz2lX0jYjo.woff\r\n-rw-r--r-- 1 andrew andrew\u00a0 18K Oct 16 15:52 ZvcMqxEwPfh2qDWBPxn6nmFp2sMiApZm5Dx7NpSTOZk.woff\r\n-rw-r--r-- 1 andrew andrew\u00a0 18K Oct 16 15:53 ZvcMqxEwPfh2qDWBPxn6nnl4twXkwp3_u9ZoePkT564.woff\r\n-rw-r--r-- 1 andrew andrew 2.1K Oct 16 15:52 merriweather.css\r\n-rw-r--r-- 1 andrew andrew 1.6K Oct 16 15:49 opensans.css\r\n-rw-r--r-- 1 andrew andrew\u00a0 33K Oct 16 15:49 u-WUoqrET9fUeobQW7jkRT8E0i7KZn-EPnyo3HZu7kw.woff\r\n-rw-r--r-- 1 andrew andrew\u00a0 32K Oct 16 15:50 xjAJXh38I15wypJXxuGMBtIh4imgI8P11RFo6YPCPC0.woff<\/pre>\n<p>388kB, eh? Wouldn&#8217;t have thought it adds up to that much, well that&#8217;s what happens when you use someone else&#8217;s web resource without thinking very much about it.<\/p>\n<p>Now I put this webfonts directory of mine on my server, for example here: <code><a href=\"http:\/\/littlesvr.ca\/grumble\/wp-content\/webfonts\/\">http:\/\/littlesvr.ca\/grumble\/wp-content\/webfonts\/<\/a><\/code> (I can protect it from external downloads if it becomes an issue, which it won&#8217;t). Replace the Google references inside the css files to my local server as well:<\/p>\n<pre>sed -i 's|http:\/\/fonts.gstatic.com\/s\/merriweather\/v8\/|http:\/\/littlesvr.ca\/grumble\/wp-content\/webfonts\/|g' merriweather.css\r\nsed -i 's|(http:\/\/fonts.gstatic.com\/s\/opensans\/v11\/|http:\/\/littlesvr.ca\/grumble\/wp-content\/webfonts\/|g' opensans.css<\/pre>\n<p>And now I will replace the two calls to &#8220;add_query_arg(&#8230;)&#8221; with:<\/p>\n<p><code>\"http:\/\/littlesvr.ca\/grumble\/wp-content\/webfonts\/opensans.css\"<\/code> and <code>\"http:\/\/littlesvr.ca\/grumble\/wp-content\/webfonts\/merriweather.css\"<\/code><\/p>\n<p>Checking what Firebug says.. done! Oh wait, why is there still one call to<br \/>\nhttp:\/\/fonts.googleapis.com\/css?family=Open+Sans&#8230;? Diggidy dig dig dig.. hm.. I think it&#8217;s a bug in the theme, one of the add_theme_support() args said &#8220;open-sans&#8221; instead of &#8220;theme-open-sans-font&#8221;. How the hell did I figure that out? I don&#8217;t even know but I think I might be good at this programming business :)<\/p>\n<p>Now all done, perfect! <\/p>\n","protected":false},"excerpt":{"rendered":"<p>I finally had enough of the old theme on this blog. I would have kept it but with WordPress 4 the fonts looked even smaller than they did before. I tried to fix it but found so many problems (starting withe a default font size set to 62.5%) that decided replacing it entirely will be &hellip; <\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[],"class_list":{"0":"entry","1":"post","2":"publish","3":"author-andrew","4":"post-970","6":"format-standard","7":"category-opensource","8":"category-safeforseneca"},"_links":{"self":[{"href":"http:\/\/littlesvr.ca\/grumble\/wp-json\/wp\/v2\/posts\/970","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/littlesvr.ca\/grumble\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/littlesvr.ca\/grumble\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/littlesvr.ca\/grumble\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"http:\/\/littlesvr.ca\/grumble\/wp-json\/wp\/v2\/comments?post=970"}],"version-history":[{"count":11,"href":"http:\/\/littlesvr.ca\/grumble\/wp-json\/wp\/v2\/posts\/970\/revisions"}],"predecessor-version":[{"id":982,"href":"http:\/\/littlesvr.ca\/grumble\/wp-json\/wp\/v2\/posts\/970\/revisions\/982"}],"wp:attachment":[{"href":"http:\/\/littlesvr.ca\/grumble\/wp-json\/wp\/v2\/media?parent=970"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/littlesvr.ca\/grumble\/wp-json\/wp\/v2\/categories?post=970"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/littlesvr.ca\/grumble\/wp-json\/wp\/v2\/tags?post=970"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}