I first created this short URL function in order to get bit.ly URLs so that people could tweet my article without having to shorten the URL themselves. Using this method has the added benefit of being able to track your readers through your bit.ly statistics, available in your account.
However, you don’t have to restrict its use to your social sharing buttons. You could use them in a number of different ways, including just displaying the link, so that your readers can use it as they please.
Create the function
Firstly, you need to create the function that will fetch the short URL for the page. In order to do this, you will need to set up an account with bit.ly and get the API key from your account settings.
Now go to your theme editor and edit your functions.php file. At the very bottom, include the following snippet inserting your bit.ly username and API key where shown:
<?php
function getBitly($url) {
$bitly = file_get_contents("http://api.bit.ly/v3/shorten?login=BITLYUSERNAME&apiKey=BITLYAPIKEY&longUrl=$url%2F&format=txt");
return $bitly;
} ?>
Now every page that is visited on your site will have a short URL created for it using your bit.ly API key.
Using and displaying the short URL
In my original tutorial, I showed you how to use the short URL to create a button for sharing the post on Twitter. However, you can use it any way you want. All you need to do is insert the following snippet to return the short URL wherever you want:
<?php $bitly = getBitly(get_permalink($post->ID)); echo $bitly ?>
For example, to display the URL at the end of the article, you could use the following snippet to allow your readers to copy the URL wherever they choose:
Short URL for this page - <?php $bitly = getBitly(get_permalink($post->ID)); echo $bitly ?>
You could also use this snippet in combination with the admin bar customisation tutorial to get a bit.ly short URL for any page and put it under the ‘Get Shortlink’ menu.
How did you use this snippet? Did you come up with an even more creative idea?


April 19, 2011 at 6:22 am
Nice post, Thanks for this information. I used bit.ly for twitter purpose only, after reading your post I come to know about this feature. Thanks a lot.
April 23, 2011 at 5:37 pm
I’ve been seeing more and more bloggers using this type of strategy on their blogs. Whether they use bit.ly or some other URL shortener, the effect is the same, and it’s pretty dang useful. There have been times that I’ve had a tab on my browser kept open to bit.ly just so I could shorten URLs for twitter or other purposes, and it became a bit of a hassle.
I love this!
Delena
April 23, 2011 at 6:13 pm
Hi Delena,
Absolutely, with sharing on character-restrictive sites (Twitter etc.) and the desire to track clicks, bit.ly links are an excellent resource and can be very handy. I’m glad you found this so helpful. Have you tried implementing it yet?
April 26, 2011 at 1:16 pm
this is so useful! can i just know how to make the short link appear as an hyperlink?
April 26, 2011 at 2:30 pm
Sure, use this and you’ll get a link displaying the short link and linking to it as well:
[php]<a href="<?php $bitly = getBitly(get_permalink($post->ID)); echo $bitly ?>"><?php $bitly = getBitly(get_permalink($post->ID)); echo $bitly ?></a>[/php]
April 26, 2011 at 2:40 pm
thank you :)
April 26, 2011 at 2:41 pm
If you encounter any issues, try this instead:
[php]<a href="<?php $bitly = getBitly(get_permalink($post->ID)); echo $bitly ?>"><?php echo $bitly ?></a>[/php]
May 9, 2011 at 7:35 am
Nice post, I’ve been using Bit.ly for some time through my social media pages (in fact I wrote a quick article about using Bitly to measure the success of a social media marketing campaign – it can be found on my site’s blog if you’re interested).
However, my only concern is for my on-page SEO. If I go swapping out my lovely keyword rich URLs for shorter ones, I may be losing some valuable keywords.
I’ll look into it, thanks!
May 10, 2011 at 7:28 am
Well, you could always negate that somewhat by making the link a nofollow link and allowing Google to instead focus on your tailored URLs.
May 10, 2011 at 7:13 am
This is a really useful tip, I came across a lot of websites where their links are being shortened, I believe that this sure makes the life of readers a lot easier when they have to share the post.
May 16, 2011 at 11:53 am
I was looking for a way or plug-in to shorten URL so it could be provided to my visitors. Glad I stumbled to this page. This is great strategy and with Bit.ly it is possible to track visitors and see stat which is necessary for promotion and marketing campaign.
Thanks Dave for step by step tutorial. Its working great and it saved lots of time.
June 7, 2011 at 5:59 pm
I am looking for a plugin that is able to shorten all URL’s in every post, or recognize a part of the domain and if it matches it shortens it. So not the URL of the post of page itself.
For example:
Post 1: domain/permalink
content
http://www.doitwithwp.com/cwhiuehduir —–> bit.ly/36284dw
Is this possible or can I at least possible just change all links in the content of the post to a bit.ly link?
June 7, 2011 at 6:57 pm
I see what you’re saying – you want the URLs in your content to automatically be shortened with bit.ly, but not the post’s permalink. Hmmm, I’m not sure. I’m sure that you could code it that way, in the same way that we’ve called for the post permalink to be returned as a bit.ly short URL, but I don’t know immediately how one would do that. Not sure if there’s a plugin for such a function or not, but it’s worth a look.
October 26, 2011 at 7:57 pm
Hello Dave I’m looking for something similar to what you provided
I have a plugin that creates a custom url based on the url and username
e.g. plugins will display mysite.com/username
is there a way to display this url above but shorter?
this is the part of the code that’s display the custom url
value=”"
If it helps I can provide the function that creates the custom url
Thanks
October 28, 2011 at 9:39 am
Hi David, well, the code didn’t appear (and it didn’t the second time your tried either), but yes, this would absolutely be possible. You just need to pass the URL you want shortened to the bit.ly function. You can create the URL in parts, using PHP variables like a username if you like as well.
March 20, 2013 at 12:46 pm
Hi David,
Out of all the tutorials on bitly with WordPress yours the clearest for a 9 month self taught WP developer with no previous php knowledge.
What I can’t figure out is how to pass the get_author to return the author profile page as the bitly link. Some WP functions have been depreciated and my idea was to capture get_author (which returns the nicename as a link) as a variable and then pass that variable to your bitly function.
I can’t make this work. Any ideas?
awordpresssite.com/author/brad — to — bit.ly/66666 (whatever’s returned by bitly)
If you can help I’d really appreciate it. Thanks
Brad
March 22, 2013 at 10:00 am
You want to use get_the_author_link instead :) http://codex.wordpress.org/Function_Reference/get_the_author_link
March 22, 2013 at 11:30 am
Thanks for the reply. Unfortunately that one returns the authors bio website URL as inputted in their profile.
I just found this as that I’ll try once back on the grid later today. I’ll let you know if it works. If you think of any other ideas it’d be most appreciated.
http://wordpress.stackexchange.com/questions/78820/getting-author-url-outside-the-loop
_______
(Excerpt of forum post I just found)
I’m trying to get the author’s URL so I changed the code to
post_author; ?>
but this doesn’t work.
____________
the_author_meta( ‘user_url’ ) will retrieve the website URL as set in the author’s profile. You need to use the_author_meta( ‘url’ ) to display a link to the author post archive:
post_author;
the_author_meta( ‘url’ , $author_id );
?>
Also note that you don’t need to echo anything, as the_author_meta() already displays it for you.
You can use get_the_author_link() function to get the url pointing to the author.
Also you can use the_author_link() to display the author link. You might get help with this.
Ex.
post_author;
$field=’user_url’;
the_author_meta( $field, $author_id );
?>
March 22, 2013 at 11:36 am
Hi Dave,
Thanks for the reply. Unfortunately that one returns the authors bio website URL as inputted in their profile.
I just found this as that I’ll try once back on the grid later today. I’ll let you know if it works. If you think of any other ideas it’d be most appreciated.
http://wordpress.stackexchange.com/questions/78820/getting-author-url-outside-the-loop
Note: Previous reply got messed up. (Php code)