How to Create Super Fast Retweet, Delicious and Facebook Share Widgets

How to Create Ultra Light Retweet, Delicious and Facebook Share Widgets

Javascript based widgets can slow down your pages to a crawl. Not only they affect the overall page loading time, but sometimes also delay the loading of the real content, which is a major turnoff for your site’s visitors.

There are very good reasons why Javascript widgets are a cause of page slow-down.

  1. Unoptimized code. Heavy usage of document.write function. Loading too many external scripts (javascript/css/images).
  2. Slow server of widget host. The widget keeps loading and in the process blocks the more important content.

Today, I’ll show you how to create custom retweet, delicious save and facebook share widgets (with share / save count in plain text) using PHP, so your pages load with lightening fast speed.

Apart from the speed benefit, the HTML markup generated by my code is simple and can be styled with CSS the way you want it!.

Retweet widget (with topsy API)

This widget will use topsy otter API to generate a retweet widget. Example on the top right of this post (custom style).

WordPress Code:

<?php
$shareUrl = urlencode(get_permalink($post->ID));
$shareTitle = urlencode($post->post_title);
$retweetNick = 'wpsplash';
$retweetInfo = json_decode(file_get_contents('http://otter.topsy.com/stats.json?url='.$shareUrl));
?>
<div class='retweet'>
	<a href='<?php echo $retweetInfo->response->topsy_trackback_url; ?>' class='count'>
		<span class='number'><?php echo $retweetInfo->response->all; ?></span>
		<span class='unit'>tweets</span>
	</a>
	<a href='http://button.topsy.com/retweet?nick=<?php echo $retweetNick; ?>&title=<?php echo $shareTitle; ?>&url=<?php echo $shareUrl; ?>' class='button'>Retweet</a>
</div>
  1. shareURL: url of the page
  2. shareTitle: title of the page which will be used as retweet text
  3. retweetNick: twitter username that will appear in retweet message of the text

PHP5 requirement since it uses json_decode function.

Delicious Save Widget

This will generate a delicious widget as seen on the bottom of this post. You can however style it as per your choice with CSS.

WordPress Code:

<?php
$shareUrl = urlencode(get_permalink($post->ID));
$shareTitle = urlencode($post->post_title);
$deliciousStats = json_decode(file_get_contents('http://feeds.delicious.com/v2/json/urlinfo/data?url='.$shareUrl));
?>

<a onclick='window.open("http://delicious.com/save?v=5&noui&jump=close&url=<?php echo $shareUrl; ?>&title=<?php echo $shareTitle; ?>", "facebook", "toolbar=no, width=550, height=550"); return false;' href='http://delicious.com/save?v=5&noui&jump=close&url=<?php echo $shareUrl; ?>&title=<?php echo $shareTitle; ?>' class='delicious'>
<?php
if($deliciousStats[0]->total_posts == 0) {
	echo 'Save';
} elseif($deliciousStats[0]->total_posts == 1) {
	echo 'One save';
} else {
	echo $deliciousStats[0]->total_posts.' saves';
}
?>
</a>
  1. shareURL: url of the page
  2. shareTitle: title of the page

PHP5 requirement since it uses json_decode function.

Facebook Share Widget

This will generate facebook share widget as seen on the bottom right of this post. Like delicious widget, you can style it with CSS.

WordPress Code:

<?php
$shareUrl = urlencode(get_permalink($post->ID));
$shareTitle = urlencode($post->post_title);
$fbLinkStats = simplexml_load_file('http://api.facebook.com/restserver.php?method=links.getStats&urls='.$shareUrl);
?>

<a onclick='window.open("http://www.facebook.com/sharer.php?u=<?php echo $shareUrl; ?>&amp;t=<?php echo $shareTitle; ?>", "facebook", "toolbar=no, width=550, height=550"); return false;' href='http://www.facebook.com/sharer.php?u=<?php echo $shareUrl; ?>&amp;t=<?php echo $shareTitle; ?>'  class='facebookShare'>

<?php
if($fbLinkStats->link_stat->total_count == 0) {
	echo 'Share';
} elseif($fbLinkStats->link_stat->total_count == 1) {
	echo 'One share';
} else {
	echo $fbLinkStats->link_stat->total_count.' shares';
}
?>
</a>
  1. shareURL: url of the page
  2. shareTitle: title of the page

PHP5 requirement since it uses simplexml library.

This is it! You got customizable Retweet, Delicious, and Facebook share widgets with share count and all, that do not slow down your page.

What icon pack are you using on WPSplash?

We are using Social Media Network Icons Pack by Komodo Media. They are beautiful!

How do I optimize X widget?

Use the API offered by the widget’s site with your favourite server-side language, PHP in case you’re using WordPress. Also, post your requests in comments. We’ll make updates to this post and send status updates via twitter. Follow WPSplash on Twitter, and subscribe to RSS feed.

23 Comments
  1. php :(
    anything for ASP.. or shuld i keep using javascript widgets!

  2. Why should it be faster to let the server fetch the data from the remote host instead of the client?

    Instead of letting the client load the widget from the widget server you do that serverside using file_get_contents().

    So if the remote services are slow your page will still load slow.

    If you however would cache those results you fetch with file_get_contents() this would be indeed a major speed improvement.

    • Hi Christiaan,

      WPSplash was running javscript based widgets on its pages. Page loading time with those were ridiculous. It kept loading until it timed out or finally fetched and parsed the data.

      One of the solution was to fetch the results server-side and completely eliminate the use of javascript. It is working. We have experienced major speed improvements and we’re happy. :)

      Your suggestion of caching the results will definitely be another major improvement. I will write an update to this article soon.

      Thanks for your suggestion!

    • I’m using all of these on one of my sites and it’s a bit slow. I’d love to see the example of caching the results.

      Any updates on this?

  3. Hey man, you got anything for the reddit upvote-downvote count? That would be really helpful. Thanks

  4. This is really awesome! Thank you so much for sharing this code, I’ve already applied it to my sites: http://oseano.net http://hilandoeldestino.com

    Only a question, since I’m not php-wise; how can I add a hashtag to the retweet code? :)

    Thank you again,

    Betty.

  5. Oh just a quick questions. I noticed when you click your facebook share button in the popup window the description seems to be an exert of this post. But when I implement your code from this post for the facebook share widget, the popup window pulls my sites meta description into the window.

    I’m just wondering how you go about making the post exert appear in the popup.

    • When a user shares your content, facebook looks at your page to find a relevant title and image, and media if you specify it. You should add tags to your page to help us find this information.

      Use AIO SEO Plugin and add custom meta description on your posts and pages where the facebook button is located.

  6. Wow…this is exactly what I was looking for. Just add StumbleUpon, Digg, and Google Buzz and my project will be complete! :) Do you know if they have comparable APIs?

    Also looking forward to the caching update.

    THANKS!

  7. @Betty
    Please use this code and don’t forget to edit the retweetNick variable.
    http://pastebin.com/cnibXmxH

  8. Thank you Haris, but I couldn’t make it work. Twitter still gives me an error message. Anyway, what worries me now is if this code will keep working after Twitter shuts down the connections to their API. What changes must be done to make this retweet code work with the OAuth? :)

    Kind regards,
    B.

  1. [User Link:How to Create Super Fast Retweet, Delicious and Facebook Share Widgets] | Tips for Designers and Developers | tripwire magazine
  2. How to Create Super Fast Retweet, Delicious and Facebook Share Widgets | pro2go Designs Blog
  3. wp-popular.com » Blog Archive » How to Create Super Fast Retweet, Delicious and Facebook Share Widgets
  4. CSS Brigit | How to Create Super Fast Retweet, Delicious and Facebook Share Widgets
  5. How to Create Super Fast Retweet, Delicious and Facebook Share Widgets | WordPress News
  6. How to Create Super Fast Retweet, Delicious and Facebook Share Widgets « Netcrema – creme de la social news via digg + delicious + stumpleupon + reddit
  7. How to Add Facebook and Twitter Share Buttons to BuddyPress Profiles - WordPress MU and BuddyPress plugins, themes, support, tips and how to's
Leave a Reply