Australia's Leading Digital Marketing Experts. T. 1300 235 433  |  Aggreagtion Enquires Welcome

Include Google’s platform.js Into Your WordPress Footer

In a number of articles we intend on publishing on our website, Googel's platform.js JavaScript code will be referenced over and over. Rather than include it in very article, we've included the code here for reference. For the sake of convenience, we've included a WordPress plugin... although using the code in your custom functions file is almost always a more prudent option.

While the code may be rendered anywhere on your page, Google recommends the footer as it may improve load time. The only exception is when using any of the JavaScript API methods; the API code needs to be placed in the page after the script inclusion.

By itself this code doesn't perform any function. Its purpose is to support various Google related posts Tag: google on our blog.

WordPress Function

Copy and paste the WordPress function into your theme's functions.php file or, if you sensibly have one installed, your custom functions plugin. You may optionally download and install our plugin from the bottom of of the page.

1
<?php 
2
/*
3
    Include Google's platform.js Into Your WordPress Footer
4
    http://www.beliefmedia.com/code/wp-snippets/google-javascript-footer
5
*/
6
 
7
 
8
function beliefmedia_google_platformjs() {
9
 echo '<script src="https://apis.google.com/js/platform.js" async defer></script>';
10
}
11
add_action('wp_footer', 'beliefmedia_google_platformjs');

■ ■ ■

Alternatives

If you were to use the dynamic code (no longer used by us for most of our shortcodes), the following will add the JavaScript to the WP footer. It is required that it be rendered after the last widget tag (the footer will do this).

1
<?php 
2
/*
3
    Include Google's platform.js Into Your WordPress Footer
4
    http://www.beliefmedia.com/code/wp-snippets/google-javascript-footer
5
*/
6
 
7
 
8
function beliefmedia_google_platformjs() {
9
?>
10
  <script type="text/javascript">
11
    (function() {
12
      var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
13
      po.src = 'https://apis.google.com/js/platform.js';
14
      var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
15
    })();
16
  </script>
17
<?php
18
}
19
add_action('wp_footer', 'beliefmedia_google_platformjs');

If adding language, or if you'reusing onload callback to execute code after all dependencies have loaded, use the following.

1
<?php 
2
/*
3
    Include Google's platform.js Into Your WordPress Footer
4
    http://www.beliefmedia.com/code/wp-snippets/google-javascript-footer
5
*/
6
 
7
 
8
function beliefmedia_google_platformjs() {
9
 echo '<script src="https://apis.google.com/js/platform.js" async defer></script>';
10
}
11
add_action('wp_footer', 'beliefmedia_google_platformjs');

More details are available on Google's developer website .

PHP Code

Used outside of WP, the function is virtually identical.

1
<?php 
2
/*
3
    Include Google's platform.js With PHP
4
    http://www.beliefmedia.com/code/wp-snippets/google-javascript-footer
5
*/
6
 
7
 
8
function beliefmedia_google_platformjs() {
9
?>
10
 <script >
11
   window.___gcfg = {
12
     lang: 'zh-CN',
13
     parsetags: 'onload'
14
   };
15
 </script>
16
 <script src="https://apis.google.com/js/platform.js" async defer></script>
17
<?php
18
}

Note: All the Google functions have changed since we first published similar code on Internoetics. It's the first JavaScript block that's made available in the downloads below.

Download

This download is no longer available.

Like this article?

Share on facebook
Share on Facebook
Share on twitter
Share on Twitter
Share on linkedin
Share on Linkdin
Share on pinterest
Share on Pinterest

Leave a comment