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

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

  31. 31 curse voice key says:
    May 17, 2014 at 1:15 am

    If your telemarketers cannot engage your prospects in animated conversations then their
    calls will not be successful. His fist met her face with a loud crack as she fell
    off the stool onto the cold hard floor below. The man fell
    to the floor holding his bloodied face in his hands but my
    father did not stop there.

  32. 32 seducing women says:
    May 31, 2014 at 11:15 pm

    Howdy! Do youu know if they make any plugins
    to help with SEO? I’m tring to get my blog to rank for some
    targeted keywords but I’m not seeing very good results.
    If you know off any please share. Thanks!

  33. 33 http://bestpoolcleanerreviews2014.com says:
    Jun 11, 2014 at 8:18 am

    I will immediately grasp your rss as I can’t to find your e-mail subscription hyperlink or e-newsletter service.
    Do you’ve any? Kindly permit me recognise in order that I could subscribe.
    Thanks.

  34. 34 best blu ray players 2014 says:
    Jun 12, 2014 at 7:05 am

    Good day! This post could not be written any better!
    Reading this post reminds me of my old room mate! He always kept talking about this.

    I will forward this post to him. Pretty sure he will have a good read.
    Many thanks for sharing!

  35. 35 gaming monitors 2014 says:
    Jun 17, 2014 at 11:56 pm

    Hi! This is kind of off topic but I need some help from an established blog.
    Is it very hard to set up your own blog? I’m not very techincal but I can figure things out pretty quick.
    I’m thinking about creating my own but I’m not sure where to
    start. Do you have any points or suggestions? Many thanks

  36. 36 paper airplanes says:
    Jun 18, 2014 at 12:02 am

    Hello my friend! I wish to say that this article is amazing,
    nice written and include almost all significant infos. I would like to
    look extra posts like this .

  37. 37 free cleaning says:
    Jun 18, 2014 at 7:25 am

    Hey! I know this is kinda off topic however , I’d figured I’d ask.
    Would you be interested in exchanging links or maybe guest writing a blog post or vice-versa?
    My website discusses a lot of the same subjects as
    yours and I think we could greatly benefit
    from each other. If you are interested feel free to send me an e-mail.
    I look forward to hearing from you! Terrific blog by the way!

  38. 38 Tabatha says:
    Jun 19, 2014 at 10:36 pm

    Hello There. I discovered your weblog the use of msn. That is an extremely neatly written article.

    I will be sure to bookmark it and come back to learn more of your helpful information.
    Thanks for the post. I’ll certainly return.

  39. 39 dental office says:
    Jun 25, 2014 at 5:38 pm

    WOW just what I was searching for. Came here by searching
    for oral hygiene

  40. 40 bestcoffeemakerreviews2014.com says:
    Jul 10, 2014 at 7:45 am

    excellent points altogether, you simply gained a emblem new reader.
    What could you recommend in regards to your post
    that you made a few days ago? Any sure?

  41. 41 best router black friday deals says:
    Jul 30, 2014 at 4:30 am

    Pretty element of content. I just stumbled upon your website and in accession capital to assert that I get in fact enjoyed account your blog posts.
    Any way I will be subscribing for your feeds and even I success you get entry
    to constantly quickly.

  42. 42 TWOHIG says:
    Jul 31, 2014 at 7:39 am

    Superb troubles completely, simply earned a fresh target audience. Exactly what may well an individual suggest regarding your upload which you manufactured some days previously? Any confident?

  43. 43 Brent says:
    Aug 14, 2014 at 11:51 pm

    Hi there, You’ve done an incredible job. I’ll definitely digg it and personally recommend to
    my friends. I’m sure they will be benefited from this web site.

  44. 44 cheat download dragon city hack says:
    Sep 04, 2014 at 6:27 am

    If you try some super site for yourself, you will see that the nature
    of games for free available is virtually limitless.
    You can find out more than 2000 games in which do work with firefox.
    During these hectic times, the free online games are really a boon as one can enjoy them anywhere, anyplace.

  45. 45 e cigarette liquid says:
    Sep 11, 2014 at 10:15 am

    Thanks designed for sharing such a pleasant thinking, paragaph is nice, thats why i have reawd it entirely

  46. 46 Reviews on Acne No More says:
    Sep 18, 2014 at 7:00 am

    I am regular reader, how are you everybody? This paragraph posted at this web site is truly good.

  47. 47 Kerrie says:
    Sep 25, 2014 at 12:13 am

    Good information. Lucky me I ran across your website by chance (stumbleupon).
    I’ve saved as a favorite for later!

    Look at my site phen375 diet, Kerrie,

  48. 48 erect penis says:
    Sep 27, 2014 at 3:55 pm

    Having read this I thought it was rather informative.
    I appreciate you taking the time and energy to put this
    article together. I once again find myself personally spending a
    lot of time both reading and leaving comments.
    But so what, it was still worthwhile!

  49. 49 Black Storm film complet says:
    Oct 21, 2014 at 4:18 pm

    Very good blog! Do you have any tips for aspiring writers?
    I’m hoping to start my own website soon but I’m a little lost on everything.
    Would you recommend starting with a free platform like WordPress or go for a
    paid option? There are so many choices out there that I’m totally overwhelmed
    .. Any suggestions? Bless you!

  50. 50 i1030.com says:
    Nov 08, 2014 at 8:34 am

    If you wish for to improve your familiarity just keep visiting this web site and be updated with
    the latest news update posted here.

Leave a Comment