WebResourcesDepot |
Faster And Fault-Proof Tweet Button Posted: 30 Aug 2010 02:58 AM PDT At WRD, the previous tweet button for sharing stories was the cool Easy Retweet Button by John Resig. However, it was not perfect as, sometimes, it was unable to communicate with the URL shortener service and the tweet links were appearing buggy. In search for a better one, I tested few others including the official tweet button. They were loading the links ok but had other issues like "loading slow", "blocking pages on load", etc. And, for a button that is clicked by -maybe- 1/100 of the visitors, loading the scripts and making requests for every visitor didn't look like the smartest choice. Here is another method, that is actually used at WRD now which loads very fast for everyone and almost fault-proof. How it works?First of all, the button is hosted locally and it does not make any requests until it is clicked.
Pros
Cons
The CodeIt is built by PHP. You'll see that the code is simple and can be ported to any other scripting language quickly. Here it is: <?php /* - A customizable, fast-loading and fault-proof tweet button. - Built by Umut Muhaddisoglu (@umutm) of http://www.webresourcesdepot.com. - Strict LTADSSIYND License (Listen To A Dire Straits Song If You Never Did) */ /* Requires Update - START */ $viaText = ' (via @umutm)'; $bitlyLogin = 'yourBitlyLogin'; $bitlyApiKey = 'yourBitlyPass'; /* Requires Update - END */ /* ******************************No Need To Update Below****************************** */ /* Getting Post Variables - START */ $postURL = urlencode($_GET['postURL']); $postTitle = html_entity_decode(htmlspecialchars_decode($_GET['postTitle'], ENT_QUOTES)); /* Getting Post Variables - END */ /* Bit.ly Shorten Function - START */ function getBitlyURL($theURL,$theBitlyLogin,$theBitlyApiKey) { return file_get_contents('http://api.bit.ly/v3/shorten?login=' . $theBitlyLogin . '&apiKey=' . $theBitlyApiKey . '&longUrl=' . $theURL . '&format=txt'); } /* Bit.ly Shorten Function - END */ /* TinyURL Shorten Function - START */ function getTinyURL($theURL) { return file_get_contents('http://tinyurl.com/api-create.php?url=' . $theURL); } /* TinyURL Shorten Function - END */ /* Shorten URL - START */ $shortenedURL = getBitlyURL($postURL,$bitlyLogin,$bitlyApiKey); if (strrpos($shortenedURL, "bit.ly") == false) { $shortenedURL = getTinyURL($postURL); if (strrpos($shortenedURL, "tinyurl") == false) { $shortenedURL = $postURL; } } /* Shorten URL - END */ /* Prepare Tweet - START */ $tweet = urlencode($postTitle) . urlencode(' - ') . $shortenedURL . $viaText; $tweetLength = strlen($postTitle . ' - ' . $shortenedURL . $viaText); $postTitleLength = strlen($postTitle); $restLength = strlen(' - ' . $shortenedURL . $viaText); $dotsMargin = 4; if ($tweetLength > 140) { $tweet = urlencode(substr($postTitle,0,140 - $restLength - $dotsMargin)) . urlencode('.. - ') . $shortenedURL . $viaText; } /* Prepare Tweet - END */ /* Redirect To Twitter - START */ header('Location: http://twitter.com/home?status=' . $tweet); /* Redirect To Twitter - END */ ?> The ButtonIt is possible to use a button you designed. At WRD, the "Official Tweet Button" images are used thinking that they will be the standard soon. If you prefer to use it too, here is the HTML-CSS for it: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Faster Tweet Button</title> <style> #tweetButton a {display:block;width:55px;height: 20px;background: url("tweetButton.png") 0 0 no-repeat;} #tweetButton a:hover {background: url("tweetButton.png") 0 -20px no-repeat;} </style> </head> <body> <span id="tweetButton"><a href="http://www.thedomain.com/tweet.php?postURL=changeWithTeURL&postTitle=changeWithTheTitle" target="_blank" rel="nofollow"></a></span> </body> </html> How To Install?Here are the steps:
For WordPress After the first 2 steps above, for the 3rd step:
Any thoughts to make it better? Special Downloads: Advertisements: Related posts |
You are subscribed to email updates from WebResourcesDepot To stop receiving these emails, you may unsubscribe now. | Email delivery powered by Google |
Google Inc., 20 West Kinzie, Chicago IL USA 60610 |
Comments (0)
Post a Comment