WordPress tutorials: Create bitly URL’s automatically

Wpsnipp – Adding the second section of code into your single.php template file will automatically create and display the bit.ly urls.Once you got the API, open your theme’s functions.php file and paste the following code:




//create bit.ly url
function bitly()
//login information
$url = get_permalink();  //generates wordpress' permalink
$login = 'imjp';    //your bit.ly login
$apikey = 'R_11882237eac772b5d6126e895a06c43f'; //bit.ly apikey
$format = 'json';    //choose between json or xml
$version = '2.0.1';
//create the URL
$bitly = 'http://api.bit.ly/shorten?version='.$version.'&longUrl='.urlencode($url).'&login='.$login.'&apiKey='.$apikey.'&format='.$format;
//get the url
//could also use cURL here
$response = file_get_contents($bitly);
//parse depending on desired format
if(strtolower($format) == 'json')
$json = @json_decode($response,true);
echo $json['results'][$url]['shortUrl'];
else //xml
$xml = simplexml_load_string($response);
echo 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;



Be Sociable, Share!

Leave a comment