Add a Google+1 Badge to Your Website

Written by David Walsh on June 30, 2011 · 34 Comments
Google+ Badge

Google's new Google+ service is taking the development world by storm, and why shouldn't it?  It's minimalistic UI, use of cutting edge web techniques, and overall usability make it Google+ one of the more impressive web applications I've seen in quite a while (I was going to say "since Google Wave", but that one didn't end up well, did it?) Google+ allows you to share photos, links, statuses, and much more;  consider it a mesh between Twitter and Facebook.

Google+ will only gain more traction, meaning that the information we share will be pushed to more and more people.  This presents an excellent opportunity to promote our websites, much in the way that Twitter and Facebook have.  Let me show you a few different ways to add the new Google+1 badge to your website.

The Simple HTML

Adding a Google+1 badge is as easy as adding a few HTML tags:

<!-- one button, uses canonical link or current URL -->
<g:plusone></g:plusone>

<!-- customized for a specific address -->
<g:plusone href="http://davidwalsh.name/google-plus-badge"></g:plusone>

<!-- include the JS file -->
<script src="https://apis.google.com/js/plusone.js"></script>

Provide the g:plusone tag the URL to share via the HREF attribute (if none is provided, the page's canonical link will be searched for and used, otherwise the current URL is used) and the desired size of button via the size attribute.  This tag should be added wherever within the page that you'd like the button to appear;  the SCRIPT tag should be found at the bottom of the page.  If you'd prefer not to dabble with the HTML yourself, Google provides a Google+1 button builder to do the dirty work for you.  Four button sizes are currently offered.

You could also use the following HTML5 code to render your button:

<div class="g-plusone" data-size="standard" data-count="true"></div>

Your button must have the g-plusone CSS class and data- attributes for options.

Deferring Loading

The HTML code above will render with the rest of the page, but what if you want to render a button on your own schedule?  No worries -- Google's API allows you to render buttons whenever you'd like:

<!-- one button, uses canonical link or current URL -->
<g:plusone></g:plusone>

<!-- Place this tag in your head or just before your close body tag -->
<script src="https://apis.google.com/js/plusone.js">
  {parsetags: 'explicit'}
</script>

<!-- Now render! -->
<script>
	gapi.plusone.go();
</script>

Using {parsetags: 'explicit'} and gapi.plusone.go() will turn your g:plusone tags into badges whenever you'd like. You can also render a specific button with the following JavaScript code:

<script type="text/javascript" src="https://apis.google.com/js/plusone.js">
	{"parsetags": "explicit"}
</script>
<script type="text/javascript">
	function renderPlusone() {
		gapi.plusone.render("plusone-div");
	}
</script>
<body>
	<a href="#" onClick="renderPlusone();">Render the +1 button</a>
	<div id="plusone-div"></div>
</body>

Full API information as well as an introductory video is available here.  There are a few more small customization options available so check them out if you're looking to specify language or play with custom rendering.

Don't miss out on another avenue for sharing your awesome web content with everyone!  Implementing Twitter, Facebook, and Google+1 sharing buttons takes 5 minutes but the amount of traffic you can gain via these referral avenues is tremendous!

Comments

  1. NIce, thanks :)

  2. Awesome, will have to get this on some of my sites then.

    Just a quick one, is there not a valid XHTML way of doing it? I am assuming data-* attributes are not classed as valid xhtml? Especially the namespaced element method.

  3. Chris July 1, 2011

    Any idea how i can edit the preview-image and excerpt that is shown in google+? Like the og:image tag in facebook open graph.

    It shows some weird and unrelated images and text from somewhere on the page…

  4. Makash July 1, 2011

    Yeah awfull !!
    Can I have a g+ invitation please ? ;)

  5. Thank you for this simple explanation.

  6. Oops, you mean a Google +1 badge, not a Google+ badge, right? Google+ is (or will be) about 8 different and bigger things than just the +1 button, but only the +1 button is currently publicly available to site developers.

  7. We used this excellent plugin Plus 1 by Steven Gray for our site eMirror – the world’s only online mirror.
    http://emirror.in/

  8. Thanks mate, your a good egg :)

    Google should really over thought long and hard about profile buttons before launching G+ – i’m rather worried.

  9. Thanks David
    One question though what if you needed multiple buttons in one page, for example in a list.
    Is it simply the case of providing different href per button?

  10. Thx for this article, however that’s a pity +1 integration breaks W3C code validity, so I wouldn’t put it on my website ^^

  11. Thanks you for the instructions. Our clients are sure to be asking for this soon, and now I am prepared!

  12. Thanks David Walsh.

    Do you know if it is possible to add a Google +1 button with pure html (no javascript)?

  13. Carsten Milkau December 1, 2011

    Text is invalid HTML (because only either src oder Text is allowed) and causes a JS error in FF7, as it tries to parse {parseTags: … } as normal JavaScript, which is doomed to fail.
    Maybe you can find a cleaner way to do this, like
    gplusone_options = {…}

    That’d be great!

  14. Carsten Milkau December 1, 2011

    Text
    is invalid HTML (because only either src oder Text is allowed) and causes a JS error in FF7, as it tries to parse {parseTags: … } as normal JavaScript, which is doomed to fail. Maybe you can find a cleaner way to do this, like
    gplusone_options = {...};

    That’d be great!

  15. Carsten Milkau December 1, 2011

    Waah, wrapped in code-Tags and still stripped -.- Last attempt with entities:

    Causing JS Error:
    <script src=”…gplusone…”> Text </script>

    Suggested improvement:
    <script src=”…”> gplusone_options = {…}; </script>
    <script src=”…gplusone…”></script>

  16. Thanks for this, I put a G+ Plus 1 Button on my webcam page. :)

  17. Hi Thanks for the post.
    I added google plus badge to my blog, it works on PCs and laptops..
    when i view my site in mobiles, G+ badge and plus one button are not shown..
    may i know the reason why its not shown?
    I love G+ because it increases your chances to show on top in search engine

  18. Can you tell me how to change we’re word with site name in google plus badge

  19. i wanna put it on floating bar… is it possible

  20. Hello,
    see i’m bit new to this. i have a question, in Webmaster Tools you get different button that to normal +1 button are they both different?

  21. can you plz tell me what is the advantages of google+ button
    and the diffrence of badge and button??

  22. Google+ badge, right? Google+ is (or will be) about 8 different and bigger things than just the +1 button, but only the +1 button is currently publicly available to site developers.Maybe you can find a cleaner way to do this, like
    gplusone_options = {…}

  23. Seems alittle bit complicated just to add the g+1 button to my website. Can’t you get this code from Google’s website?

  24. Very useful script.. i will definitely use one of theses!

  25. Hello,
    I have a static website and i copy pasted this code in the source but its not showing anything on the page.
    i have no programming knowledge so i don’t know what i am doing.

  26. Thank you it works great on my website.

  27. Hi,

    Any one any idea how to get a Google Plus Side Follow Widget which is Vertical in nature and when hover pops a little and then customer can share the site.

    Thanks & regards

  28. The latest badges for Google plus are really nice and fit snuggly in the sidebar.

Be Heard

Tip: Wrap your code in <pre> tags or link to a GitHub Gist!

Use Code Editor
Older
Google+ Invites
Newer
Create a Google+ Mac App for Your Dock