Automatically Create Short URLs for Posts in Tweets

Automatically Create Short URLs for Posts in Tweets

With a simple hack to the theme-specific “functions.php” file, WordPress can automatically generate and display short URLs that are perfect for inclusion in Tweets. This hack uses an existing URL shortening service, and comes in two parts. The first part, placed into the functions file, looks like the example below:

function makeBitly($url) {

$tinyurl = file_get_contents(“http://bit.ly/api-create.php?url=”.$url);

return $bitly;

}

With that new function added to “functions.php,” a second piece of code must be added to the website’s template in order to display the created URL. That line of PHP code must be placed within the WordPress Loop, and it looks like the following:

1

< ?php $burl = makeBitly(get_permalink($post->ID)); echo ‘The short URL for this post: < a href=”’.$burl.’” >‘.$burl.’< /a >‘ ? >