Scroll Down
/**
* code #20 - get TinyURL for any post or page via shortcode tag
* source - http://wordpress.stackexchange.com/a/5741/8922
* ex: [tinyurl] will output the TinyURL for the current page
* [tinyurl url="http://google.com/" title="google"] will output the TinyURL for Google.com
*/
function custom_tinyurl_shortcode($atts){
extract(shortcode_atts(array(
'url' => get_permalink(),
'title' => ''
), $atts));
if(!$title) $title = $url;
if(false === ($cache = get_transient(‘tinyurl_’+md5($url)))):
$cache = wp_remote_retrieve_body(wp_remote_get(‘http://tinyurl.com/api-create.php?url=’.$url));
// cached for 24 hours
set_transient(‘tinyurl_’+md5($url), $cache, 60*60*24);
endif;
return ‘<a href=”‘.esc_url($cache).'”>’.esc_attr($title).'</a>’;
}
add_shortcode(‘tinyurl’, ‘custom_tinyurl_shortcode’);
Many WordPress developers are creating shortcodes to embed into content. This makes it easier for writers to create full HTML widgets like video players and image slideshows. This snippet will create a shortcode which you may call in two ways. The first is simply [tinyurl] which will output the TinyURL version of the current page.
However you can pull a remote URL as a TinyURL by adding some optional parameters. The title=”” parameter will display link text inside the tag. Otherwise your output text will be the same as the new TinyURL. Check out my comments in the source code for some more detailed examples.