Every webmaster loves su.pr, the reason for that is when someone clicks on your short link it displays the page and a sidebar in which are the popular content from your site. It also track the number of clicks to the link so it’s great tool. I use it for twitter button, you can see it at the end of this post.
Anyway if you want to shorten the url and display the short url on the page you can do that with some php code. There are 5 api methods to do that: shorten, simpleshorten, post and schedule. First two are just shrinking the url, last two are shrinking url and posting it to twitter or facebook or both. Schedule can be setup to post later. If you want to want to display the url I don’t suggest using post or or schedule because every time the user visits the page it will post a tweet, if your followers click the link it will produce a new tweet and so again and again.
Every method except simpleshorten returns JSON object. I will show how to get the short url using shorten method. First if you’re not registrated at su.pr I suggest you do that because you will not be able to track number of clicks to link if you are not registrated.
Here is the PHP code insert it somewhere before the place you want your link to show, it will not post anything just get the link:
<?php
$title = the_title('', '', false);
$title_enc = urlencode($title);
global $post;
$permalink=get_permalink($post->ID)
$posturl="http://su.pr/api/shorten?longUrl=".$permalink."&login=username&apiKey=yourapi";
require_once(ABSPATH . 'wp-includes/class-snoopy.php');
$snoopy = new Snoopy;
$snoopy->fetchtext($posturl);
$results = json_decode($snoopy->results, true);
$shrink = $results['results'][urldecode($permalink)]['shortUrl'];
?>
Instead of username and yourapi put your username and apikey for su.pr.
If you wont to use su.pr short links on your own domain to have something like yourdomain.com/asdf, read instructions on su.pr site and also there is now a bug at su.pr that adds su.pr/ inside of the short link, so my link looks like http://su.pr/skepo.info/p8BR instead of http://skepo.info/p8BR. You can fix it by adding the line:
$shrink = str_replace("su.pr/", "", $shrink);
After the line line
$shrink = $results['results'][urldecode($permalink)]['shortUrl'];
Later when you need to show short link just paste this in your theme file:
<a href="<?php print($title_enc); ?>">Short link</a>
If you have any trouble try entering in the browser http://su.pr/api/shorten?longUrl=URL&login=USERNAME&apiKey=APIKEY, just change the URL, USERNAME and APIKEY. The JSON object you receive is easy to read and it will be easy to understand why it’s not working.
If you want to use short link using shortcode go to your theme folder and find functions.php and paste this code at the and before ?> :
function supr_link($attrs){
$title = the_title('', '', false);
$title_enc = urlencode($title);
global $post;
$permalink=get_permalink($post->ID);
$posturl="http://su.pr/api/shorten?longUrl=".$permalink."&login=username&apiKey=yourapi";
$snoopy = new Snoopy;
$snoopy->fetchtext($posturl);
$results = json_decode($snoopy->results, true);
$shrink = '<a href="'. $results['results'][urldecode($permalink)]['shortUrl'] . '">Short link</a>';
$shrink = str_replace("su.pr/", "", $shrink);
return $shrink;
}
add_shortcode('supr', 'supr_link');
Now you can use [supr] to show the shortlink to the current page. Again you need to enter you username and apikey, change the text Short link on 10th line to something you want. If you’re not using su.pr on your own domain delete 11th line.
Here is an example: Short link











Programming is like sex: one mistake and you’re providing support for a lifetime.


