Shortcode: Automatically create a short url for Twitter

Add following code where you want to display it.

Usage

[shorturl name="shortcode" url="http://codex.wordpress.org/Shortcode_API"]

Add following snippet to “functions.php”

Snippet

add_shortcode('shorturl', 'subzane_shorturl');
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>';
	}
}

Courtesy: Subzane

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>