How to use Bitly to generate short URLs and track your social media clicks.
In this quick tutorial, I will show you how to automatically generate Bitly short URLs within WordPress. We are going to use their latest API v4 which was released just a month ago, at the beginning of March 2020.
I will also show you a couple of practical ways to integrate those URLs within your WordPress to track your social media clicks.
There is a whole lot more you could do with Bitly just go their API documentation page and take a look.
Let’s get started.
Before I show you any code you need to create and login into your Bitly account and generate access token by going to My Account > Profile Settings > Generate Access Token.
Once we have our token we need to add an action when we publish a new post to make an API call, generate a Bitly short URL and add it as a post meta within our database.
Note: you need to find a way to republish all your posts if you add this feature when you already have existing posts. You should probably find a free plugin for this within the WordPress.org plugin directory.
And voilà, now when you publish a new post it will automatically generate a Bitly link (you can view this in your Bitly account).
WordPress already has a short-link method built-in and usually shows your URL in the Plain form e.g. https://ctrls.dev/?p=123.
Since we have a new short-link URL it’s a good idea to overwrite the built-in method. To do this first we need to remove the existing method from the head and then with a couple of actions add our new method.
If you take a look at your post source code inside the head tag for shortlink meta now you will see something like this:
<link rel="shortlink" href="https://bit.ly/3bg4Rqs" />
Note: I am also adding a fallback method where we use the plain URL format.
Once we do this when we call the wp_get_shortlink($post_id) we won’t get the plain WordPress permalink but the new Bitly short URL.
Here is an example of how you could include your Bitly link inside your content.
<a href="<?php echo wp_get_shortlink($post->ID);?>" title="<?php the_title(); >"><?php echo wp_get_shortlink($post->ID);?></a>
In addition, you may want to access and view the Bitly short URLs within your WordPress admin area. To do this you need to extend your Post columns (you can apply the same for Pages or custom Posts) by adding a couple of actions with manage_posts_*.
There are many more things you can do with the Bitly API, especially when it comes to statistics and tracking. I searched the WordPress.org plugin directory and there are only a few Bitly related plugins. So if you want to build an extended plugin with statistics features you could give it a try.
If you have premium Bitly account you can even have your own short domain mapped and instead of bit.ly you can have your links to look like this https://ctrls.dev/3bg4Rqs
Lastly, I will leave you with one more idea that you can think about. Use the Bitly links with your Post share URLs.
I have done that in my Blog and you can view the result when you click on the Share icon at the bottom left corner of each post.
<a href="https://www.facebook.com/sharer.php?u=https://ctrls.dev/3bg4Rqs">Share on Facebook</a>
‘Till the next one.
* We have a NO SPAM policy so you won't reaceive any meaningless emails from us!