Create bit.ly Short URLs for Use Anywhere in Your Theme

Creating usable links is vital to having your content shared

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:

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:

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:

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?

19 thoughts on “Create bit.ly Short URLs for Use Anywhere in Your Theme”

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

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

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

  3. andrea says:

    this is so useful! can i just know how to make the short link appear as an hyperlink?

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

  4. andrea says:

    thank you :)

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

  5. Patrick says:

    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!

    1. Well, you could always negate that somewhat by making the link a nofollow link and allowing Google to instead focus on your tailored URLs.

  6. Wez Bailey says:

    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.

  7. John Winson says:

    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.

  8. Roberto says:

    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?

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

  9. David says:

    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

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

  10. Brad says:

    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

      1. Brad says:

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

      2. Brad says:

        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)

Leave a Reply