Create 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 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 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 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 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 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 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 Short URLs for Use Anywhere in Your Theme”

  1. Nice post, Thanks for this information. I used 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 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 just so I could shorten URLs for twitter or other purposes, and it became a bit of a hassle.

    I love this!


    1. Hi Delena,

      Absolutely, with sharing on character-restrictive sites (Twitter etc.) and the desire to track clicks, 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 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 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 —–>

    Is this possible or can I at least possible just change all links in the content of the post to a link?

    1. I see what you’re saying – you want the URLs in your content to automatically be shortened with, 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 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
    is there a way to display this url above but shorter?
    this is the part of the code that’s display the custom url
    If it helps I can provide the function that creates the custom url

    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 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? — to — (whatever’s returned by bitly)

    If you can help I’d really appreciate it. Thanks


      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. 


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

        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.


        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. 

        Note: Previous reply got messed up. (Php code)

Leave a Reply