Automatically Create a Bit.ly URL for WordPress Posts

Automatically Create a Bit.ly URL for WordPress Posts

Bit.ly is currently the most popular URL shorting service out there and for a good reason too. With bit.ly you can track your shortened URLs and much more. In this article I'm going to show you how to use bit.ly's api create bit.ly urls automatically for WordPress posts.

In order to make use of Bit.ly's API, you'll need to:

  1. Signup at Bit.ly
  2. Get your API Key

Now that you have a login and API key, open your WordPress theme's functions.php (just create one if you don't have one) and paste the following code at the top of the document:

//create bit.ly url
function bitly()
{
	//login information
	$url = get_permalink();  //generates wordpress' permalink
	$login = 'imjp';	//your bit.ly login
	$apikey = 'R_11882237eac772b5d6126e895a06c43f'; //bit.ly apikey
	$format = 'json';	//choose between json or xml
	$version = '2.0.1';

	//create the URL
	$bitly = 'http://api.bit.ly/shorten?version='.$version.'&longUrl='.urlencode($url).'&login='.$login.'&apiKey='.$apikey.'&format='.$format;

	//get the url
	//could also use cURL here
	$response = file_get_contents($bitly);

	//parse depending on desired format
	if(strtolower($format) == 'json')
	{
		$json = @json_decode($response,true);
		echo $json['results'][$url]['shortUrl'];
	}
	else //xml
	{
		$xml = simplexml_load_string($response);
		echo 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;
	}
}

The code above is pretty much self explanatory.
Don't forget to change the login and apikey strings to match yours.

I've modified David Walsh's "Create Bit.ly Short URLs Using PHP" in order to generate bit.ly urls automatically with WordPress. Now you can display your bit.ly url anywhere inside the loop or post .

The result of <?php bitly(); ?> in this post is http://bit.ly/4UbQ66.

I hope you guys found this article useful :D!

Related Articles



Tags: , ,

19 Comments

  1. 1 Inside the Webb says:
    Jan 24, 2010 at 12:23 pm

    It’s cool to see everything you can do with API’s these days. Even a simple URL shortening system allows you to work with their API and PHP programming to create custom shortened URL’s. Pretty neat!

  2. 2 JP says:
    Jan 24, 2010 at 12:27 pm

    yep, I got tired of manually creating bit.ly links and I didn’t want to use tinyurl links(there are quite some tutorials out there to implement tinyurl with wordpress) because I want to track the bit.ly links.

  3. 3 Simon says:
    Feb 20, 2010 at 8:28 pm

    Great post, I’m going to add this feature to my site shortly. Although I think I’ll use a hook so I can port it between my themes easily (and add it to the RSS feed!).

    Do you know if this works with the bit.ly Pro service, which allows you to use custom domains?

  4. 4 David Wells says:
    Feb 21, 2010 at 9:23 pm

    Very cool indeed!

    I am having a problem with the though. It is calling the same bit.ly link no matter the page where I input the code.

    For some reason it is not pulling the exact page I am on.

    Any ideas? Thanks =)

  5. 5 David Wells says:
    Feb 21, 2010 at 9:38 pm

    It’s not calling the for the right page

  6. 6 Vadim.Gukoilov says:
    Mar 20, 2010 at 6:42 am

    Меня все полностью устраивает, поэтому и молчу.

  7. 7 Zerikole says:
    Mar 20, 2010 at 2:04 pm

    Да я тоже согласен с Вами, действительно для людей.

  8. 8 JP says:
    Mar 22, 2010 at 9:16 am

    @David: Make sure that the < ?php bitly(); ?> is inside the loop. If it is outside it’ll be the same link no matter what.

  9. 9 mbt fuaba says:
    Jul 21, 2010 at 11:13 am

    I really enjoyed this post, especially the “examples in this post” portion which made it really easy for me to SEE what you were talking about without even having to leave the article. Thanks

  10. 10 escort escort says:
    Jul 21, 2010 at 7:47 pm

    Rather nice article to spend some time on reading it to my thinking. I have a question, why haven’t you you add that article to social bookmarking sites? That might bring much traffic to this domain.

  11. 11 buy tea online says:
    Jul 25, 2010 at 4:05 am

    very useful information and i have found it for a long time,thanks so much.

  12. 12 portable keyboards says:
    Mar 27, 2011 at 3:45 am

    Thank you for sharing this information. I have been searching for instructions for how to use bit.ly’s api to create bit.ly urls for WordPress posts, for a long time.

  13. 13 studio monitors says:
    Apr 29, 2011 at 12:34 am

    I found this information particularly useful for all my online marketing. Thank you very much for sharing this information.

  14. 14 JP says:
    May 05, 2011 at 6:53 pm

    Glad I could help guys :)

  15. 15 frenky says:
    May 07, 2011 at 4:49 pm

    3UURlR http://gdjI3b7VaWpU1m0dGpvjRrcu9Fk.com

  16. 16 Dustin says:
    May 19, 2011 at 8:43 am

    I’m modifying a template for a friend and I’m trying to figure out how to use your php function so that when my wordpress post gets tweeted it uses a bit.ly url rather than the long form url.

    Does that make sense?

    Also, how do I know if the function is working?

  17. 17 Asian Tv says:
    Jul 05, 2011 at 3:14 am

    Cool thing! CAn ypu perhaps explain how to use that shortlink (in my case a pro custom url short link) in the side sharebar much like you have here on your site too?

    Cheers

  18. 18 Millie says:
    Aug 26, 2011 at 3:33 pm

    “open your WordPress theme’s functions.php (just create one if you don’t have one)”..

    Could you tell us HOW to do this?

  19. 19 JP says:
    Sep 12, 2011 at 5:04 am

    @Millie: You need to go into the theme directory and open up the file named functions.php (or create it if it doesn’t exist) and add the code provided in the tutorial in it.

    Feel free to contact me if you have any other issues.

Leave a Comment