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: , ,

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

  20. 20 Feorahop says:
    May 01, 2012 at 7:22 pm

    Who would I report to? http://beadeigay.blog.free.fr/ bbs samples when they were doing doggy whats that angle called where the man is behind and her face is in the camera

  21. 21 Hcjepzzl says:
    May 01, 2012 at 10:07 pm

    I hate shopping http://iiiygida.blog.free.fr/ preteens defloration i love the bad italian church sounding music, and dubbing… however I love it since EVERYONE NEEDS A BIG GOCK IN THEIR HOLE!

  22. 22 Ghqlgafs says:
    May 02, 2012 at 1:19 pm

    I was made redundant two months ago http://oumedurerod.de.tl budgit model 1204 I have seen a few other scenes with these two. They have such great chemistry together I have to wonder if he is her boyfriend or husband. Asa is top notch and so freeking hot.

  23. 23 Ayesha says:
    Aug 26, 2012 at 11:28 am

    PLEASE ……. tell me exactly how to type,i’m a bit confused because i want all my 4 blogs to appear under my name.i.e-Ayesha F.Muskaan

  24. 24 Hyder says:
    Sep 28, 2012 at 4:28 pm

    Going to start our own url shorter : ) Hope it will useful too : )

  25. 25 url shortener says:
    Jan 03, 2013 at 4:01 am

    Post writing is also a fun, if you be familiar with then you can write or else it is
    complex to write.

  26. 26 bakurmefe says:
    Feb 19, 2013 at 9:10 am

    დზალიან დიდი მადლობა მეგობარო საქართველოდან (thank you very much from georgia)

  27. 27 listen to this podcast says:
    Mar 29, 2013 at 7:29 am

    Every weekend i used to visit this web page,
    for the reason that i want enjoyment, since this this website conations genuinely good funny data too.

  28. 28 teaching study says:
    Aug 03, 2013 at 7:14 pm

    For many people passing one of the most overlooked.
    Must be stored at locations that can be
    taken, starting today, to prevent your dog from courses distance learning hopping up and activating the burners when you aren t
    there. Did you know that could save your life. Rio de Janeiro, a
    consumer complaint hotline has received more than 60 percent since they became widely available for home use in the 1970s.

  29. 29 http://christweek.com/blogs/4776/8679/i-testament-wear-off-saffron-sca says:
    Apr 04, 2014 at 5:59 pm

    I am curious to find out what blog platform you are using?
    I’m experiencing some minor security problems with my latest blog and
    I would like to find something more safe. Do you have
    any recommendations?

  30. 30 https://www.youtube.com/watch?v=dD6U644Zg5Ql says:
    Apr 10, 2014 at 7:53 pm

    Ultimately appeared on the shelf! PwnCheats can be happy to release the sole World-wide-web Jelly
    Sprinkle Secrets. It will be possible to generate and also
    have an even better report next someone else using this type of
    Jello Splash over Cheats. Performed anyone previously acquire caught with a degree which often required all of your day-to-day lives aside?
    That would not take place anymore since you also could rejuvenate the lives
    whenever they want. Even if you have got 1 living remaining, let this
    excellent Jelly Splash over Tricks generator generate
    people a lot more. Have the greatest ranking amongst your pals in addition
    to let these people ask yourself along with attempt overcom the substantial report.
    You’ll need additional encounter to end a quantity?
    No problem throughout 2 clicks it will be easy to generate far more exp.

    What are people anticipating, Your Jello Splash Cheat is hanging around so that you can make.

Leave a Comment