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:


You can send a querystring to Twitter like: Tweet. For example, a tweet link for this post can be: 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:

function makeShortURL($URLToConvert) { 
     $shortURL= file_get_contents("" . $URLToConvert); 
     return $shortURL; 

So, calling the function with the URL, like:

<?php echo makeShortURL("")?>

will result in:

And the link to this post will become: To Add Tweeting With Short URL Functionality To A Website (With PHP):

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="<?php the_title(); ?>: <?php echo makeShortURL(get_permalink())?>" rel="nofollow">Tweet This Post</a>.

And that’s all.

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

  • 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.

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

  • This is a great tip. Thanks for sharing.

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

    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.

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

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

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

    Sorry that I never used Tweetburner.

  • Gracias, me ha funcionado muy bien. 🙂

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

  • Thanks for sharing. How about using . Is it possible?

  • Thanks!

    This was one major headache for my blog!

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

    //off to modify some php code… 🙂