Shortcode: Automatically create a short url for Twitter

Add following code where you want to display it.


[shorturl name="shortcode" url=""]

Add following snippet to “functions.php”


add_shortcode('shorturl', 'subzane_shorturl');
function subzane_shorturl($atts) {
		'url' => '',
		'name' => '',
    ), $atts));
    $request = '' . 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>