Snippets

Joey Hernandez Add Google Fonts Asynchronously

Created by Joey Hernandez

File functions.php Added

  • Ignore whitespace
  • Hide word diff
+<?php
+/**
+ * Adding Google fonts asynchronously because it blocks page loading if loaded old fashion way
+ */
+function add_fonts_asynchronously() { ?>
+
+	<script>
+		WebFontConfig = {
+			classes: false,
+			events: false,
+			google: {
+				families: ['Open+Sans+Condensed:300,700', 'Open+Sans:400,600,700']
+			},
+		};
+
+		(function(d) {
+			var wf = document.createElement('script');
+			wf.src = ('https:' == document.location.protocol ? 'https' : 'http') +
+				'://ajax.googleapis.com/ajax/libs/webfont/1.5.18/webfont.js';
+			wf.type = 'text/javascript';
+			wf.async = 'true';
+			var s = document.getElementsByTagName('script')[0];
+			s.parentNode.insertBefore(wf, s);
+		})(document);
+	</script>
+
+	<?php
+}
+add_action( 'wp_head', 'add_fonts_asynchronously', 0 );
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.