Usare URL abbreviati è sicuramente molto utile quando i nostri URL raggiungono una lunghezza ragguardevole. Vediamo come usare il servizio offerto da Bit.ly in WordPress.
Possiamo creare uno shortcode aggiungendo il seguente codice al file functions.php
, ovviamente usando il nostro username e la nostra chiave delle API di Bit.ly:
function get_bitly_url($url) {
$content = file_get_contents("http://api.bit.ly/v3/shorten?login=LOGIN&apiKey=APIKEY&longUrl=".$url."&format=xml");
$element = new SimpleXmlElement($content);
$bitly = $element->data->url;
if($bitly){
return $bitly;
} else {
return '0';
}
}
function bitly_url($atts, $content) {
extract( shortcode_atts( array(
'url' => '',
'title' => '',
), $atts ) );
$bitly_url = get_bitly_url($url);
return '<a href="' . $url . '" title="' . $title . '">' . $bitly_url . '</a>';
}
add_shortcode('bitly-link', 'bitly_url');
Possiamo usare lo shortcode come segue:
[bitly-link url="http://path/to/file/" title="Test"]