Create bit.ly Short URLs for your Posts in WordPress

I have been working with the http://bit.ly API and decided to change the short URLs on my site to include the short URLs provided by http://bit.ly. The API is pretty straightforward and I created a few WordPress functions to to get it all working.
Before we even start, you would need to registered with http://bit.ly and then check out this page http://bit.ly/a/your_api_key to get your API key and your login.
Now, let’s add a function to automatically create a http://bit.ly short URL when you publish a post. We will hook into the publish_post action and then use the http://bit.ly API to fetch a shortened URL. Place the following into your theme’s function.php file.

add_action('publish_post', 'create_bitly');
function create_bitly($postID) {
	global $wpdb;
	$login = 'YOUR LOGIN'; // change this to your login
	$apikey = 'YOUR API KEY'; // change this to your API key
	$longURL = get_permalink($postID); // here we get the permalink to your post
	// This is the API call to fetch the shortened URL
	$apiurl = 'http://api.bit.ly/v3/shorten?longUrl='.urlencode($longURL).'&login='.$login.'&apiKey='.$apikey;
	// We are using cURL
	$curl = curl_init();
	curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($curl, CURLOPT_URL, $apiurl);
	$results = json_decode(curl_exec($curl));
	curl_close($curl);
	$shortURL =  $results->data->url; // the short URL
	update_post_meta($postID, 'bitlyURL', $shortURL); // adding the short URL to a custom field called bitlyURL
}

You can read more about the http://bit.ly API here: http://code.google.com/p/bitly-api/wiki/ApiDocumentation
In order to add your new short URL to your header, you first need to remove the default shortlink tag and then create a function that will place the proper code between your <head> tags.

remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 ); // removing the default shortlink
add_action( 'wp_head', 'bilty_shortlink_head'); // adding the new bit.ly shortlink
function bilty_shortlink_head() {
	global $post;
	$shortURL = get_post_meta($post->ID, 'bitlyURL', true);
	if(!empty($shortURL)) {
		echo ''."\n";
	} else {
		echo ''."\n";
	}
}

If you want to display your new short URLs, you can do so by using the `wp_get_shortlink()` function. But first you need to make sure that WordPress knows to replace the default short URL with the new http://bit.ly short URL.

add_filter('pre_get_shortlink', 'get_bitly_shortlink'); // filtering the WP function
function get_bitly_shortlink() {
	global $post;
	$shortURL = get_post_meta($post->ID, 'bitlyURL', true);
	if(!empty($shortURL)) {
		 return $shortURL;
	} else {
		return get_bloginfo('url').'?p='.$post->ID;
	}
}

You can place the following code anywhere within the loop of your single.php theme file to display your short URL.

Short URL: ID); ?>

Share this:

Email
Facebook
Twitter
Pinterest
Pocket

Premium Themes for WordPress

Looking for an easy-to-use Premium Theme for WordPress? Check out Themes by bavotasan.com and have your site up and running in no time.

Use this WordPress website builder to build powerful websites in no time for your or your clients.

WordPress Hosting

WP Engine – designed from the ground-up to support and enhance the workflow of web designers.

Bluehost – providing quality web hosting solutions since 1996.

About the author

Picture of Luke Perrie

Luke Perrie