Home / functions.php / Create bitly URL’s automatically
  • How to Create bitly URL’s automatically

    no thumbnail sorry: Create bitly URL’s automatically

    Add the first section of code into your functions.php. Adding the second section of code into your single.php template file will automatically create and display the bit.ly urls.

    ( click code to copy )

    wordpress snippet : PHP - functions.php

    <>
    //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;
    	}
    }
    
    ( click code to copy )

    wordpress snippet : PHP - single.php

    <>
    <?php
               bitly();
    ?>
    

    ( WordPress codex functions, hooks, in this snippet. )

    get_permalink,