ADD GOOGLE PLUS COMMENTS TO YOUR WEBSITE


SUBMITTED BY: Guest

DATE: May 3, 2013, 9:20 a.m.

FORMAT: Text only

SIZE: 5.4 kB

HITS: 1166

  1. What you need:
  2. 1. Google+ API key. You must create your own API key and in the settings arrange you can use it on your domain. Fix it to your domain, or else someone else might take it and use your quota (not likely to happen soon).
  3. Prepare your website:
  4. 2. Add jQuery to your website (if you haven't already).
  5. The most simple way is to use Google's script that is even hosted on CDN. You can add it like this:
  6. <script type="text/javascript" src="http://www.google.com/jsapi"></script>
  7. <script type="text/javascript">
  8. google.load("jquery", "1.7.1");
  9. </script>
  10. If you wish, you can complicate by adding just some parts of it to configure faster load times. Not really reccomended. :)
  11. 3. Add jQuery templates to your website.
  12. Again simply just add this to the head section:
  13. <script src='http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js'
  14. type='text/javascript'></script>
  15. This time, Microsoft CDN. Isn't life amazing? This is beta though. Haven't checked if there is already a better version, this one just worked. And later on in our lifes, this will most probably work natively in jQuery.
  16. 4. Add this script anywhere in your website html.
  17. The best is somewhere near the </body> so that the browser doesn't read it early. Change the html tags if you feel you must (remove the comment date for example).
  18. <script id="GooglePlusStreamTemplate" type="text/x-jquery-tmpl">
  19. <li id="${id}">
  20. <div class="CommentAuthor">
  21. <a href="${actor.url}" target="_blank">
  22. <img class="actorPhoto" src="${ photoManipulate(actor.image.url,100) }" />
  23. </a>
  24. </div>
  25. <div class="CommentHeader">
  26. <a href="${actor.url}" target="_blank">
  27. <div class="CommentAuthorName">${actor.displayName}</div>
  28. </a>
  29. <div class="CommentDateTime">${updated}</div>
  30. </div>
  31. <div class="CommentContent">
  32. {{html object.content}}
  33. </div>
  34. </li>
  35. </script>
  36. 5. Add following javascript anywhere in the html.
  37. The best is though with you website main scripts if you will use comments in every page/post. All you need to modify is aipKey. But if you wish, you may change other settings (especially imgHeight and imgWidth). Or even improve the code if you feel like hacking up. :)
  38. <script type='text/javascript'>
  39. var GooglePlusPH = "GooglePlusPH";
  40. var GooglePlusST = "GooglePlusStreamTemplate";
  41. var GooglePlusStreamData = null;
  42. var apiKey = "YourAPIkey";
  43. var activityid = "z12xydka1uepd3d2u22szz0xdwulybioh04";
  44. var imgHeight = 150;
  45. var imgWidth = 150;
  46. $(document).ready(function () {
  47. if ($("#" + GooglePlusPH).length > 0) {
  48. activityid = $("#" + GooglePlusPH).attr("data-activityid");
  49. $.ajax({
  50. url: "https://www.googleapis.com/plus/v1/activities/" + activityid + "/comments?alt=json&maxResults=100&fields=items(actor%2Cid%2Cobject%2Fcontent%2Cpublished%2Cupdated)%2CnextLink%2CnextPageToken&key=" + apiKey + "&callback=?",
  51. data: "format=json", jsonp: "jsoncallback", dataType: "jsonp",
  52. success: function (data) { GooglePlusStreamData = data; showGooglePlusStream(); }
  53. });
  54. }
  55. });
  56. function showGooglePlusStream() {
  57. $("#" + GooglePlusST).tmpl(GooglePlusStreamData.items).appendTo("#" + GooglePlusPH);
  58. gapi.plusone.go();
  59. }
  60. function photoManipulate(imgUri, imgNewSize) {
  61. return imgUri.replace("sz=50", "sz=" + imgNewSize);
  62. }
  63. </script>
  64. 6. Add and customize following css:
  65. <style type="text/css">
  66. .GooglePlusComments { padding:0px;clear:both; }
  67. .GooglePlusComments li { list-style-type: none; width:100%; margin-bottom:20px; display:block;clear:both; }
  68. .CommentAuthor { float: left; margin-right: 10px; width: 100px; }
  69. .CommentAuthor img { width:100px;height:100px;margin-bottom:5px;border: solid 2px white; }
  70. .CommentHeader a { text-decoration: none; float: left; margin-right: 10px; color: #00628B; }
  71. .CommentDateTime { }
  72. .CommentAuthorName { font-weight:bold; }
  73. .CommentHeader{margin-bottom:5px}
  74. .CommentContent { text-align:justify;color:#000000; }
  75. </style>
  76. Start binding comments to posts
  77. So now that you have your website ready, you just need to connect your posts. This solution works by connecting individual posts 1 <=> 1. So if you are writing about the same thing on Google+ and your website.
  78. 7. Get Google+ post id
  79. This is the trickiest part. If anyone finds easier solution, please let us know. :) You have to open the post on Google+ from which you wish to have the comments. The post we will open for this case is this one.
  80. Chrome users: Click on top of the post with your right mouse button and select "Inspect element"
  81. In other browsers it might be simmilar, the important thing is that you come to the html part of the post.
  82. There you will find div with id of the post: <div id="update-z13xtzyrqwqtvhw2q22szz0xdwulybioh04"...
  83. Copy this post id (just the blue part, without update-).
  84. 8. Finally, put this html where you want the post comments and change the post id:
  85. <ul id="GooglePlusPH" data-activityid="z131it5zdumksj0dv22szz0xdwulybioh04" class="GooglePlusComments"></ul>

comments powered by Disqus