How To Add Tweeting With Short URL Functionality To A Website (With PHP)

Social bookmarking buttons like Digg, Delicious or StumbleUpon on any website is very familiar. They help spreading the content.

Twitter, getting bigger & better, is also another powerful network where content can reach to thousands easily in minutes.

To help the readers of a website share/tweet a content at Twitter easily, here is a simple way of integration:

Twitter

You can send a querystring to Twitter like:

http://twitter.com/home?status=Your Tweet. For example, a tweet link for this post can be:

http://twitter.com/home?status=How To Add Tweeting With Short URL Functionality To A Website (With PHP): http://www.webresourcesdepot.com/how-to-add-tweeting-with-short-url-functionality-to-a-website-with-php/

The problem & the solution:

There is a 140 character limit in Twitter & the length of the URL is a headache. So, auto-shortening the URL with a short URL service will be handy.

The file_get_contents function of PHP helps us to easily get the short URL as a string. Lets use the TinyURL service & create a simple function:

<?php
function makeShortURL($URLToConvert) { 
     $shortURL= file_get_contents("http://tinyurl.com/api-create.php?url=" . $URLToConvert); 
     return $shortURL; 
}
?>

So, calling the function with the URL, like:

<?php echo makeShortURL("http://www.webresourcesdepot.com/how-to-add-tweeting-with-short-url-functionality-to-a-website-with-php/")?>

will result in: http://tinyurl.com/arbz2e.

And the link to this post will become:

http://twitter.com/home?status=How To Add Tweeting With Short URL Functionality To A Website (With PHP):http://tinyurl.com/arbz2e

For WordPress users:

Wordpress LogoWordPress users can add the makeShortURL function to the functions.php in the active theme folder.

Important, make sure you don’t leave any spaces between other functions & at the bottom of the functions.php file. Else, the RSS feed will have some trouble in creating the XML files.

And, in the single.php file, where the posts are displayed, use the line (or update it to your needs) below:

<a href="http://twitter.com/home?status=<?php the_title(); ?>: <?php echo makeShortURL(get_permalink())?>" rel="nofollow">Tweet This Post</a>.

And that’s all.

  • http://greenflipflops.com Gabe Harris

    Awesome tutorial! I love getting exposed to API’s that I wouldn’t find on my own (tinyURL here), thanks for the good work.

  • http://www.lonelygrill.com Paul Grill

    Remember that using the urlencode() – function may save you the trouble you will experience with more complicated URLs and/or special characters like the ampersand.

  • http://dtsn.co.uk Daniel

    Have you guys seen the tweetmeme button? http://tweetmeme.com basically provides all the functionality without coding anything, plus it gives you a count of the number of times you link has been twittered.

  • http://www.faridhadi.com Farid Hadi

    This is a great tip. Thanks for sharing.

  • http://www.webresourcesdepot.com Umut M.

    @Paul,
    Yes, you;’re definitely right, I’ll add urlencode() function.

    @Daniel,
    Tweetmeme is great. However you may not want to include a remote file, its design may not fit you or you may create something totally new. Else, it is very functional.

  • http://dtsn.co.uk Daniel

    @Umut you could still use the API to gather the tweet count

  • http://www.twitter.com/MillerMosaicLLC Yael K. Miller

    Do you know to do these using someone’s TweetBurner account?

  • http://www.webresourcesdepot.com Umut M.

    @Daniel,
    Didn’t know that Tweetmem had an API, good to hear.

    @Yael,
    Sorry that I never used Tweetburner.

  • http://miipodtouch.com repacheco81

    Gracias, me ha funcionado muy bien. :)

  • http://www.zamshed.info Zamshed Farhan

    This link is very helpful for beginner to make link with twitter.

  • http://www.Jelbee.tk/ Jelbee

    Thanks for sharing. How about using bit.ly . Is it possible?

  • http://www.ipodgaming.net/ iPod Gamer

    Thanks!

    This was one major headache for my blog!

    @Paul
    Thanks for the tip! Would never have thought of that!

    //off to modify some php code… :-)

Search