2031

How to add a tweet this button with auto TinyURL or Bit.ly shortening

wordpress tips tricks

How to add a Tweet This button to your wordpress posts without using any plugin or widget? Well there are few sites allow you to do that. But for instance I want the control to be in my hand and it’s pretty much no point of using a plugin or widget to do this kind of job. Why? because each time a plugin/widget loads, it takes away cpu and ram resources from server. So let me show you a nifty simple way to add a cool Tweet This button to your wordpress posts including automatic creation of TinyURL/Bit.ly url for your post. The automatic TinyURL creation for wordpress post already published at WPRecipes website but it was not implemented in their Tweet This post options. Additionally I’ve added Bit.ly API usage code by my own. Bit.ly dynamic creation of urls offers statistics and usage information (free registered account). Let me show you the implementation of that in a more useful manner.

First of all in your wordpress theme directory you should have a functions.php file which contains common functions to be used inside your themes. In case you don’t have one, create a file called functions.php inside your favorite wordpress theme directory. Now open the functions.php file and add this function which will allow you to create TinyURL.com urls on demand.

Code to generate tinyurl inside your themes functions.php:

// Ondemand function to generate tinyurl
function getTinyUrl($url) {
     $tinyurl = file_get_contents("http://tinyurl.com/api-create.php?url=".$url);
     return $tinyurl;
}

or you can add bit.ly JSON API to create dynamic bit.ly urls on the fly. Bit.ly offers tracking features for the urls generated. This is a little bit more tricky (Note: this requires an user account at http://bit.ly and a API key, Once you login to your Bit.ly account you can find it here). The sample code below (don’t forget to change the login name and api key)

// Ondemand function to generate dynamic bit.ly urls
function getBitlyUrl($url) {
    // fill up this 2 lines below with your login and api key
    $bitlylogin = 'yourlogin';
    $bitlyapikey= 'yourapikey';

    // you dont need to change below this line
    $bitlyurl = file_get_contents("http://api.bit.ly/shorten?version=2.0.1&longUrl=".$url."&login=".$bitlylogin."&apiKey=".$bitlyapikey);  

    $bitlycontent = json_decode($bitlyurl,true);

    $bitlyerror = $bitlycontent["errorCode"];

    if ($bitlyerror == 0){
        $bitlyurl = $bitlycontent["results"][$url]["shortUrl"];
    }
    else $bitlyurl = $url;

    return $bitlyurl;
}

Ok once you are done with the code, save and close the functions.php file and open your single.php file which is your wordpress single post view template. Now you can add the piece of code underneath the function called the_content(). Add this sample code underneath there to generate an image with the link to post to twitter and additionally append the post title and post tinyurl along with it. See below.. here is the sample of a single.php file.

<!-- looking for the_content() -->
<?php the_content(); ?> 

<div class="SocialIcons">
<!-- Note Below -->
<a target="_blank" title="Share this article with your Twitter followers" href="http://twitter.com/home?status=Reading%3A+<?php the_title(); ?> - <?php
 $turl = getTinyUrl(get_permalink($post->ID));
 echo $turl;
 ?> (via @ruhanirabin)" rel="nofollow" class="social-bookmark"><img alt="Tweet this!" src="<?php bloginfo('template_directory'); ?>/twitter.png"/></a>

</div>

Note that the above code must have the proper image file (twitter.png file in your theme directory) to display it properly. Also you can switch the function names to getTinyUrl to getBitlyUrl.

Now this is how it looks like when you click on the button and it opens twitter to post the status entry. The entire line is generated from the button itself including the tinyurl link.

Now this is how it looks like when you click on the button and it opens twitter to post the status entry. The entire line is generated from the button itself including the tinyurl link.

As you can see above that you don’t really need any 3rd party widget or plugin to accomplish the Job because it’s pretty fast to do and you are in control of the look and feel, not to mention you can save your resources. For a live example you can use the Twitter icon from my Social media icons bar.

Alternative Reading

There is another good post about this at labnol.org. It’s the javascript based chiklet implementation of this similar topic.

GD Star Rating
loading...
How to add a tweet this button with auto TinyURL or Bit.ly shortening9.7106

About Ruhani Rabin

Ruhani Rabin is the original owner and author of this blog. He also reviews web 2.0 startups at Tech2all.com. Largely interested in web 2.0 apps & Social Media. Currently the Vice President of MOL Access Portal (MOL is owner of Friendster.com). Also he is Web 2.0 & Social Media Researcher and a Total fun Geek!.. There you have it ;)

Twitter Profile: http://twitter.com/ruhanirabin

  • There's also a service that does a Tweet This button with automatic URL shortening at http://ko.ly (no php coding required)!
  • Thanks for this - I was looking for a non-plugin alternative to TweetMeme and this fits the bill perfectly. Nice and light.

    I did find that I had to remove the headupmark tags in order for it to work properly but just now looking at the code above noticed that there is a ";" after the "<" bracket that follows the "status=" section of the code. Is that what was messing it up? Do you need to use these tags? The code seems to work without them.

    Dave
  • Quick fix - the code breaks when the URL contains Unicode characters.

    To fix that, use your favorite Unicode-compatible URL encoding function before sending the URL to bit.ly, and when parsing the response, replace:

    $bitlyurl = $bitlycontent["results"][$url]["shortUrl"];

    with:

    $temp = array_values($bitlycontent["results"]);
    $bitlyurl = $temp[0]["shortUrl"];

    This removes the dependency on both bit.ly and you encoding the URL the same way.
  • Ori
    Thank you so much for this! It did exactly what I needed and nothing more, and gave me the control I wanted. Excellent.
  • I tried your code a number of times on my site, in order to replace the current "Tweet this" link already in my theme.

    Unfortunately, despite my effort, it never worked. And the page stopped loading once it got to that part of the code. I had the relevant addition to my theme functions.php and here's what I was editing into the single.php

    <a target="_blank" title="Tweet this" href="http://twitter.com/home?status=Reading%3A+<?php the_title(); ?> - <?php $turl = getTinyUrl(get_permalink($post->ID)); echo $turl; ?> (via @jamesbrobinson)" rel="nofollow" class="social-bookmark"><img src="<?php bloginfo('template_directory'); ?>/images/social-twitter.gif"alt="Tweet this!">

    What have I missed please?
  • thank u for sharing. this one the interesting article which i read related to short url website. thanks again for this.
  • Leslie
    Any guidance on how to add in the 2nd tranche of this code when using the Atahualpa theme with no single.php file? Thanks.
  • Look for index.php file and apply the code similar as described above.
  • Leslie
    Can you give any guidance for how to add the 2nd tranche of code when using the Atahualpa theme which has no single.php file?
  • aint there a way to add that to a regular html website ?
blog comments powered by Disqus