With u.nu being the producer of the worlds shortest urls, period, I thought it might be a good idea to create a little Wordpress plugin that would allow me to shorten my urls using shortcode.
The shortcode
The shortcode in this example is simple. We want to send the url as a parameter as well as the name of the link. However the name parameter will be optional.
[shorturl name="shortcode" url="http://codex.wordpress.org/Shortcode_API"]
The plugin source code
This is the source code of the actual plugin. As I said before we have two parameters. Url and name. However name is optional so in case name is not provided or blank we wish to print out the url as link text instead. Also if the service returns something else than a url we simply just print the original url.
function subzane_shorturl($atts) {
extract(shortcode_atts(array(
'url' => '',
'name' => '',
), $atts));
$request = 'http://u.nu/unu-api-simple?url=' . urlencode($url);
$short_url = file_get_contents($request);
if (substr($short_url, 0, 4) == 'http') {
$name = empty($name)?$short_url:$name;
return '<a href="'.$short_url.'">'.$name.'</a>';
} else {
$name = empty($name)?$url:$name;
return '<a href="'.$url.'">'.$name.'</a>';
}
}
add_shortcode('shorturl', 'subzane_shorturl');
That’s it really. Very quick and simple and there’s probably more interesting ways this can be implemented or used. But this is just a simple example to get you going. Good luck!
Important
If you get weird results try deactivating plugins that search for words. I had to deactivate the “Acronyms” plugin because it would skip the name parameter on several links otherwise. I have no idea why though.


