Automatically Shorten your Permalink using Bit.ly services


Sponsored Links




WordPress Beginner show nicely tutorial about How To Automatically Shorten your Permalink using Bit.ly services. Why we need to shorten our URL? Shorten URL is good to share on many social web, social news, social media and social network such as Twitter, Facebook, Digg and many more..

Shorten your Permalink using Bit.ly - Marie Digby Photo

Shorten your Permalink using Bit.ly - Marie Digby Photo


Before we began using this tutorial, you need create your own Bit.ly API Key on this page.. after finish paste this code on your functions.php

<?php
/* Based on code from David Walsh – http://davidwalsh.name/bitly-php */
function make_bitly_url($url,$format = 'xml',$version = '2.0.1')
{
//Set up account info
$bitly_login = 'YOUR LOGIN HERE';
$bitly_api = 'YOUR API KEY HERE';
//create the URL
$bitly = 'http://api.bit.ly/shorten?version='.$version.'&longUrl='.urlencode($url).'&login='.$bitly_login.'&apiKey='.$bitly_api.'&format='.$format;
//get the url
$response = file_get_contents($bitly);
//parse depending on desired format
if(strtolower($format) == 'json')
{
$json = @json_decode($response,true);
return $json['results'][$url]['shortUrl'];
}
else //For XML
{
$xml = simplexml_load_string($response);
return 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;
}
}
?>

You need to change your LOGIN and API key on the code above. After finish, open your single.php and paste this code under start on your WordPress Loop

<?php
//Check for post's shortened URL. Used with twitter feedback.
if(get_post_meta($post->ID, "short_url", true) != ""){
//Short URL already exists, pull from post meta
$short_url = get_post_meta($post->ID, "short_url", true);
}else{
//No short URL has been made yet
$full_url = the_permalink();
$short_url = make_bitly_url($full_url);
//Save generated short url for future views
add_post_meta($post->ID, 'short_url', $short_url, true);
}
?>

And the last thing to do, paste this code anywhere on your loop

<?php echo $short_url; ?>

I hope this tutorial work on you, if you have any question you can discuss it here or contact WordPress Beginner.

Subscribe to Full RSS Feed

RSS FeedIf you thing this article useful, please consider subscribing to our RSS Feed or e-mail updates and stay updated with us. You can also follow @WPTricksNet on twitter for latest updates.

About WP Tricks

We are WordPress Family, we collected and writed useful tips and tricks for you WordPress. We share it for you...

Related Tips and Trick from Local Data and on The Net

How to Automatically Shorten Your WordPress URLs with Bit.ly

Services; Subscribe; Explore; Google+ we will show you how to automatically shorten your WordPress Posts URLs using Bit.ly API. been made yet $full_url = the_permalink Read More

WordPress: Auto URL Shortening | Fuel Your Coding

Shortening feature within your WordPress theme using the bit.ly API. get_contents('http://api.bit.ly/v3/shorten No, this only create’s a bit.ly link out of the permalink Read More

8 Websites To Shorten Your Tweets Automatically

Links: such as to shorten long URLs with tweetitin.com. Your messages will be shrunk automatically in URL’s with unhub: using bit.ly services Bookmark the permalink. Post Read More

bitly blog

26, 2011 | Permalink plugin allows you to automatically shorten links in incoming comments, and to do so with your own bit.ly sharing via social services Read More

How to Send Wordpress Posts to Twitter | eHow.com

blog entry, although it won't shorten the URL automatically. WP to Twitter will shorten the URL using the Cli.gs or Bit.ly services Currently reading Read More


4 Responses to Automatically Shorten your Permalink using Bit.ly services

  1. Social Media says:

    great, thanks! I’m already using bit.ly for tweets, but it’s good to know, how to automatically short urls in my wordpress blog.

  2. Pingback: Automatically empty Trash | Unlock IMEI Cellphones

  3. EuroVPS says:

    Can I use another short URL service (like tiny.cc) with your code? What changes do I have to make?

  4. sydney jobs says:

    The Shorten URL is good to share on many social web, social news, social media and social network.

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>