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?