Add a Google+1 Badge to Your Website
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="https://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!
NIce, thanks :)
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.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…
Not yet, but I’ll investigate.
still trying to solve that too. but did not get any information yet. also, something similar to Facebooks URL Linter would be nice during development. I will post solution here on success.
Any update on whether og:image or image_src works for Google Plus? I’m currently pulling out what little hair I have left trying to figure this out.
I’m also wondering whether Google Plus has a URL/link cache that needs clearing.
Try passing page url like this:
gapi.plusone.render(‘element’, {‘href’: ‘example.com’);
Also see +Snippet:
https://developers.google.com/+/plugins/+1button/#plus-snippet
Did any one find how to over ride unrelated images from page with single image
Did any one find how to avoid unrelated images from page
Yeah awfull !!
Can I have a g+ invitation please ? ;)
Thank you for this simple explanation.
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.
Yep, updated the language.
We used this excellent plugin Plus 1 by Steven Gray for our site eMirror – the world’s only online mirror.
http://emirror.in/
Thanks mate, your a good egg :)
Google should really over thought long and hard about profile buttons before launching G+ – i’m rather worried.
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?
Thx for this article, however that’s a pity +1 integration breaks W3C code validity, so I wouldn’t put it on my website ^^
Thanks you for the instructions. Our clients are sure to be asking for this soon, and now I am prepared!
Thanks David Walsh.
Do you know if it is possible to add a Google +1 button with pure html (no javascript)?
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!
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!
Waah, wrapped in code-Tags and still stripped -.- Last attempt with entities:
Causing JS Error:
Suggested improvement:
Thanks for this, I put a G+ Plus 1 Button on my webcam page. :)
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
Can you tell me how to change we’re word with site name in google plus badge
i wanna put it on floating bar… is it possible
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?
can you plz tell me what is the advantages of google+ button
and the diffrence of badge and button??
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 = {…}
Seems alittle bit complicated just to add the g+1 button to my website. Can’t you get this code from Google’s website?
Very useful script.. i will definitely use one of theses!
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.
Thank you it works great on my website.
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
The latest badges for Google plus are really nice and fit snuggly in the sidebar.
The code is not visible under mobile devices. Tried with latest Android and iOS version. Any help would be appreciated.
Regards