Function to Change TinyURL Shortcode Tags

* code #20 - get TinyURL for any post or page via shortcode tag
* source -
* ex: [tinyurl] will output the TinyURL for the current page
* [tinyurl url="" title="google"] will output the TinyURL for
function custom_tinyurl_shortcode($atts){
'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(‘’.$url));
// cached for 24 hours
set_transient(‘tinyurl_’+md5($url), $cache, 60*60*24);

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.