Categories
Advanced Tutorials

Integrate Bitly URLs with WordPress

How to use Bitly to generate short URLs and track your social media clicks.

This quick tutorial will automatically show you how to generate Bitly short URLs within WordPress. We will use their latest API v4, which was released just a month ago, at the beginning of March 2020.

I will also show you some 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 to 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 an access token by going to My Account > Profile Settings > Generate Access Token.

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://krasenslavov.com/?p=123.

Since we have a new short-link URL, overwriting the built-in method is a good idea. First, we need to remove the existing method from the head and then add our new method with a couple of actions.

If you take a look at your post source code inside the head tag for short link meta now, you will see something like this:

<link rel="shortlink" href="https://bit.ly/3bg4Rqs" />

Note: I am also adding a fallback method using 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 including 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 regarding 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 a premium Bitly, you can even have your own short domain mapped, and instead of bit.ly, you can have your links look like this https://krasenslavov.com/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://krasenslavov.com/3bg4Rqs">Share on Facebook</a>

‘Till the next one.

Leave a Reply

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