Integrate Bitly Short URLs Within WordPress

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.

Generate Bitly Access Token

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.

Subscribe to our Newsletter* if you find our content useful and would like to receive regular updates with the latest posts.

Add a Hook When You Publish New Posts

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

What’s Next?

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>

Manually Access Bitly Short URLs

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_*.

In Conclusion

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!

Related Content

Adding License Key Feature to Your Premium WordPress Theme or Plugin (2/3)

How to set up our API server and link up the previously created functions with our server app.

READ MORE...

Create Gutenberg Blocks with ACF 5.8+ PRO

How to register and customize Gutenberg blocks with the Advanced Custom Fields plugin (no JavaScript needed)

READ MORE...

Track your WordPress.org Plugin Stats on Your Site

How to track your WordPress.org plugins statistics and display them as a widget within your own theme.

READ MORE...

One response to “Track your WordPress.org Plugin Stats on Your Site”

  1. Bony Martinezsays:

    I loved this tutorial, thank you so much, it was very helpfull.
    Please, post the Github repository with the finally working code, it will help all us to compare our code with yours and to debug possible problems we face.
    Thank you so much for all and keep the good work 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

Adding License Key Feature to Your Premium WordPress Theme or Plugin (2/3)
Overwrite and Send Custom Emails in WordPress

Thank you for reading our content. Use the buttons below to spread the love and share Track your WordPress.org Plugin Stats on Your Site with your followers.