Automatically Create a URL for WordPress Posts

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

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

  1. Signup at
  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 url
function bitly()
	//login information
	$url = get_permalink();  //generates wordpress' permalink
	$login = 'imjp';	//your login
	$apikey = 'R_11882237eac772b5d6126e895a06c43f'; // apikey
	$format = 'json';	//choose between json or xml
	$version = '2.0.1';
	//create the URL
	$bitly = ''.$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 ''.$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 Short URLs Using PHP" in order to generate urls automatically with WordPress. Now you can display your url anywhere inside the loop or post .

The result of <?php bitly(); ?> in this post is

I hope you guys found this article useful :D!

Related Articles

Tags: , ,


  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 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 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 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 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’s api to create 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


  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 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?


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

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

  51. 51 Manie says:
    Dec 21, 2014 at 2:24 am

    It’s going to be finish of mine day, but before end I am reading this
    enormous piece of writing to increase my knowledge.

  52. 52 Camilla says:
    Jan 02, 2015 at 1:46 pm

    I enjoy looking through an article that can make men and women think.

    Also, thank you for allowing for me to comment!

  53. 53 Antje says:
    Jan 09, 2015 at 8:42 pm

    I do not leave a comment, but after reading a ton of comments on this page Automatically Create a URL for WordPress Posts | DevMoose.
    I actually do have 2 questions for you if you do not mind.
    Is it only me or does it seem like some of these remarks appear like they are left by brain dead
    visitors? :-P And, if you are posting on other social sites,
    I would like to follow everything new you have to post. Would you post a list of all of all
    your public sites like your Facebook page, twitter feed, or linkedin profile?

  54. 54 ถ่ายวีดีโองานแต่ง says:
    Jan 13, 2015 at 2:05 am

    Hiya very nice web site!! Man .. Beautiful ..
    Amazing .. I’ll bookmark your website and take the feeds also?

    I am glad to seek out numerous helpful info here in the
    put up, we need develop extra strategies in this regard, thank you
    for sharing. . . . . .

  55. 55 Astrid says:
    Jan 19, 2015 at 2:12 pm

    Hello to every body, it’s my first go to see of this webpage; this webpage contains remarkable and genuinely good information in favor of visitors.

  56. 56 gaming headset review in 2015 says:
    Jan 20, 2015 at 9:51 am

    I leave a leave a response when I like a article on a site or
    if I have something to add to the conversation. It is
    a result of the fire communicated in the article I browsed.
    And on this article Automatically Create a URL for
    WordPress Posts | DevMoose. I was excited enough to drop a thought ;-) I actually do have 2 questions for you if you do not mind.
    Is it only me or do a few of these comments appear as if they are
    written by brain dead individuals? :-P And, if
    you are writing on other places, I would like to follow
    anything fresh you have to post. Could you list the complete urls of
    all your communal pages like your twitter feed, Facebook page or linkedin profile?

  57. 57 Best diaper bags 2015 says:
    Jan 23, 2015 at 4:55 am

    As the admin of this website is working, no uncertainty very rapidly it will be famous, due to its feature contents.

  58. 58 soi keo hot ngay hom nay says:
    Jan 29, 2015 at 12:43 am

    He missed second-to-final match tip bong da europa league sang mai against Romania, Turkey,

  59. 59 Juvesiio says:
    Jan 29, 2015 at 1:29 pm

    Hi, i feel that i noticed you visited my weblog so i got here to go back the prefer?.I am
    trying to in finding issues to improve my web site!I assume its ok to use a
    few of your ideas!!

  60. 60 hack a facebook account says:
    Jan 30, 2015 at 7:34 am

    Just use the tool plus let our automated system dangerous the security of the online messenger for you personally, it’s as easy as that!

  61. 61 jual seragam drumband says:
    Feb 01, 2015 at 1:45 am

    If you desire to obtain a good deal from
    this paragraph then you have to apply these techniques to your won weblog.

  62. 62 Best bluray player 2015 says:
    Feb 02, 2015 at 9:46 pm

    Every weekend i used to visit this site, because i wish for enjoyment, for the reason that this this
    site conations in fact pleasant funny stuff too.

  63. 63 What are the best laptops for college says:
    Feb 03, 2015 at 4:43 am

    whoah this weblog is fantastic i love reading your posts.
    Stay up the good work! You realize, lots of persons are hunting round for this information, you
    could aid them greatly.

  64. 64 miglior aspirapolvere says:
    Feb 03, 2015 at 4:32 pm

    I don’t even know the way I finished up right here, however I believed this publish was once great.
    I don’t realize who you might be but definitely you are
    going to a famous blogger in the event you are not already.

  65. 65 My Boat Building Plans says:
    Feb 03, 2015 at 6:12 pm

    My brother recommended I may like this web site. He was once totally right.
    This submit truly made my day. You can not imagine just how so
    much time I had spent for this information! Thanks!

  66. 66 top mirrorless cameras of 2015 says:
    Feb 05, 2015 at 1:35 pm

    Thanks for the auspicious writeup. It actually was once a entertainment account it.

    Glance complex to more delivered agreeable from you!

    However, how could we communicate?

  67. 67 Advanced Cleanse Plus Aloe Review says:
    Feb 06, 2015 at 9:03 am

    Very quickly this web page will be famous amid all blog people, due
    to it’s nice posts

  68. 68 Meilleure centrale vapeur says:
    Feb 11, 2015 at 1:11 pm

    If you desire to improve your familiarity just keep visiting this website
    and be updated with the most recent gossip posted here.

  69. 69 says:
    Feb 14, 2015 at 4:50 pm

    If some one needs expert view concerning running a blog then i propose
    him/her to pay a visit this web site, Keep up the pleasant job.

  70. 70 Meilleur lisseur says:
    Feb 18, 2015 at 4:45 am

    I am in fact glad to read this website posts which includes tons of valuable data,
    thanks for providing these data.

  71. 71 miglior microonde classifica says:
    Feb 18, 2015 at 12:15 pm

    I am regular visitor, how are you everybody? This post posted at this site is genuinely

  72. 72 cheap coffe makers says:
    Feb 22, 2015 at 7:41 am

    Hello my family member! I wish to say that this article is amazing,
    nice written and include approximately all vital infos.
    I’d like to peer extra posts like this .

  73. 73 Prolong says:
    Feb 23, 2015 at 11:52 pm

    Thanks for another fantastic post. Where else may anyone get that kind
    of info in such a perfect means of writing?
    I’ve a presentation subsequent week, and I am on the search
    for such information.

  74. 74 Liproxenol says:
    Feb 25, 2015 at 11:31 am

    Thanks for finally talking about > Automatically Create a URL for WordPress Posts | DevMoose < Loved it!

  75. 75 foot covers says:
    Feb 26, 2015 at 11:20 pm

    It’s really a nice and useful piece of information. I’m satisfied that
    you shared this useful information with us. Please stay us informed
    like this. Thank you for sharing.

  76. 76 Nutra Tosterone Review says:
    Feb 27, 2015 at 1:32 pm

    It’s going to be finish of mine day, however before finish I am reading this great post to increase my

  77. 77 What are the best over ear headphones says:
    Feb 28, 2015 at 3:26 pm

    I have read so many articles or reviews about the blogger lovers but this post is actually a good
    article, keep it up.

  78. 78 Copy and Paste Formula Scam says:
    Feb 28, 2015 at 5:37 pm

    I am truly grateful to the owner of this web site who has shared
    this great piece of writing at at this time.

  79. 79 booster car seats compared says:
    Feb 28, 2015 at 10:23 pm

    Hi, this weekend is good in support of me, since this moment i am reading this impressive educational post here at my house.

  80. 80 กระเป๋าเป้ says:
    Mar 01, 2015 at 4:53 am

    Hello to every body, it’s my first pay a visit of this
    web site; this web site consists of remarkable and genuinely
    excellent stuff designed for readers.

  81. 81 Slim Trim 2000 Diet says:
    Mar 01, 2015 at 12:10 pm

    WOW just what I was looking for. Came here by searching for php

  82. 82 Testosterone Booster Supplement says:
    Mar 01, 2015 at 1:55 pm

    What you posted was actually very logical.
    But, consider this, suppose you added a little content?
    I ain’t saying your content isn’t solid, but suppose you added a headline to maybe grab
    folk’s attention? I mean Automatically Create a URL for WordPress Posts | DevMoose is a little plain.
    You might peek at Yahoo’s home page and note how they create article headlines to grab viewers
    to click. You might add a related video or a related
    picture or two to grab readers interested about everything’ve written. Just my opinion, it might make your
    blog a little bit more interesting.

  83. 83 onlien Success Plan says:
    Mar 01, 2015 at 11:33 pm

    My brother suggested I would possibly like this website.
    He was entirely right. This put up actually made my day.

    You can not imagine just how much time I had spent for
    this information! Thank you!

  84. 84 Idol White Teeth Wthiening says:
    Mar 02, 2015 at 10:24 am

    If you wish for to obtain much from this paragraph then you have
    to apply such methods to your won blog.

  85. 85 Synapsyl Brain says:
    Mar 02, 2015 at 11:33 pm

    What’s up to all, it’s actually a nice for me to visit this web page, it contains useful Information.

  86. 86 Gh advanced Review says:
    Mar 03, 2015 at 4:49 am

    It’s remarkable for me to have a web page, which is helpful
    in support of my experience. thanks admin

Leave a Comment