In this article tripwire magazine provides a tutorial on how the jQuery based Social Bookmarking Counters on the site was created. Most people are generally busy and this means that readers on a blog or website don’t time to waste on articles that are not really good. The main principle in web 2.0 and social services is to leverage upon the opinion of the community. Showing the post popularity on services that most of your users use will provide them with voulnable information when quick decisions on what to read next has to be made. I believe this kind of functionality is good and I also see it used in different variations on more and more blogs. [demo]
Advertisement
Introduction
Most of the popular web 2.0 social bookmarking services like digg, reddit, dzone etc. have buttons you can integrate into your site.
Fx. the button above showing how many time the url for the actual page have been posted on twitter. The tweetmeme button simply inserted by pasting the script below into your site:
These buttons provide the number of diggs, bookmarks etc. through a javascript that is loaded from their site behind the button. For most this is basically ok but if you want to customize the way each button looks your options are limited or none (as you know they are all very different and may not fit into you look and feel). Also by using the buttons provided your site is downloading a javascipt that you have no control of and on each request the count your articles are retrieved…this may be really time consuming and affect performance especially on article lists like a blog front page. Besides offering buttons that can easily be integrated into blogs and websites some of the social bookmarking providers also expose web services that can be called fx. using php or javascript to retreive the bookmark count for an artcile. This is the method that will be demonstrated in this article. In order to avoid the entire website to slow down while counters are retreived in php this solution use jQuery to load in the results dynamically after the pages are fully downloaded. It also use some really basic file caching to further optimize performance (and to avoid hitting the web services to hard without any reason). The solution described in this tutorial should be extendable to cover more social bookmarking service providers but currently it only includes Tweetmeme, delicious and digg. Working source code can be downloaded here. All code will not be listed in the article but so if you want to dive deeper start with downloading the files. Be aware that the code structure and architecture of the solution is not that beautiful at all but it works well and mainly serves as proff of concept for this tutorial. The solution can be illustrated at a high level as follows. A] After page is fully loaded jQuery is used to load the page in the B] socialbookmarks solution that returns a html formatted output. B] caches it’s output into a file and returns the content of the cache file if the cache file for a specific url is not older than the cache time to live. B] calls services at the social bookmarks providers C]to produce it’s output. 
1. Server side
Start with creating a folder called “socialbookmarks” in the root of your website (must be on a server that supports php5). In this folder you should copy all files from the downloaded archive. The following files are fundamental to the solution and will be described in a bit more detail below.
- index.php
- digg.badge.inc.php
- delicious.badge.inc.php
- tweet.badge.inc.php
index.php
This is the page that will be loaded on your website using jQuery. This page implements the caching and ensures that digg, delicious etc. is called and that the html that needs to be returned is build. Cache files are placed in “socialbookmarks/cache” and is named using the following pattern: cache__[url including querystring params]. Cache window is set to 2 hours. Down make the cashe time to short as tweetmeme only allows a very limited number af requests every hour. With busy site this will not work without the caching and a cacheing window of 1-2 hours.
2. Client side
The webpage that shows the social bookmarking badges need to be updated with the following code.The code make use of jQuery that is ideal for loading content up behind the scenes.
<script type="text/javascript">
$(document).ready(function(){
$('#bookmarkscontainer').html('<p><img src="ajax-loader.gif"/></p>');
$('#bookmarkscontainer').load("index.php?permalink=<?php echo rawurlencode($permalink) ?>&post-title=<?php echo rawurlencode($postTitle) ?>");
});
</script>
In the files you can download I have included a test page “testpage.php” that you can use as an example of how the bookmark counter can be used. If you have not downloaded it yet you can do it here.
3. The final result
This is the final result that you also can see all over tripwire magazine. Here is also a demo of the page being loaded dynamically into the WordPress pages [demo].

If you find this article useful please comment or bookmark the page Thanks!
Want to keep on surfing? Have look at these Drupal 7 Themes maybe this is what you’re looking for. Also don’t miss these best wordpress themes if you’re looking for a great theme for your website.









{ 14 comments… read them below or add one }
thank you …
I have already seen it somethere…
Thank you
Rufor
The article is unfortunately broken in Safari 4/Mac
After the first sentence in the introduction the article stops and the sidebar collapses beneath the comment form (instead of the usual right position). Hope you’re able to fix this
(read it in Firefox alreay, thanks for the good article!)
Thanks for notifying me. Not good not good I’m sorry for the inconvenience! I removed the script to the popular Tweetmeme button placed after the first line in the Introduction section and the page now load fine in my safari and in windows IE6.0 that alsocouldn’t present the page properly.
I’m really dissapointed that the Tweetmeme script can deliver that kind of damage! I know that it is used on http://mashable.com/ and I just tested it in Safari 4 Beta and it works fine…? strange! Anyway I added one of their buttons again and now it also works in my article so I guess the script I was using was somehow messed up.
Excellent tutorial. Thanks a ton for putting this together.
Any chance you could include scripts for fetching the StumbleUpon, Reddit, and maybe Yahoo Buzz counts?
Travis thanks for the feedback. I’ll certainly will consider taking another look at this and bring a part two of the tutorial.
Great write up! I will bookmark this have to try implicating it this weekend. Great site too!
Thanks for sharing this great post and keep it up!
Thanks a ton for putting this together.
Many bookmarking websites use Jquery. Thanks for sharing nice tutorial.
Thanks!
This works great! Using it on my site!
Mostly people use the same jquery. I have already know about this code.
Anyway, thanks for sharing the post.
Now blogs or social website visitor do not want to waste time at social bookmarking website. So therefore, webmasters use J Query in Social bookmarking website to save the visitors time.
Thank you for this great post… great job done!
Robert!
Fatal error: Cannot use object of type stdClass as array in /home/tripwir/public_html/socialbookmarks-demo/reddit.badge.inc.php on line 23
{ 7 trackbacks }