Utilizar por defecto el acortador de URLs de Google (goo.gl)

Wordpress Logo

Para utilizar este acortador solo debes incluir lo siguiente en el archivo functions.php:

[php]
function googl_shortlink($url, $post_id) {
global $post;
if (!$post_id && $post) $post_id = $post->ID;

if ($post->post_status != 'publish')
return "";

$shortlink = get_post_meta($post_id, '_googl_shortlink', true);
if ($shortlink)
return $shortlink;

$permalink = get_permalink($post_id);

$http = new WP_Http();
$headers = array('Content-Type' => 'application/json');
$result = $http->request('https://www.googleapis.com/urlshortener/v1/url', array( 'method' => 'POST', 'body' => '{"longUrl": "' . $permalink . '"}', 'headers' => $headers));
$result = json_decode($result['body']);
$shortlink = $result->id;

if ($shortlink) {
add_post_meta($post_id, '_googl_shortlink', $shortlink, true);
return $shortlink;
}
else {
return $url;
}
}

add_filter('get_shortlink', 'googl_shortlink', 9, 2);
[/php]

Luego añadir el siguiente código en donde quieras utilizar la URL corta de tu post, probablemente en el archivo single.php de tu theme:

[php]
<?php echo "URL corta: " . wp_get_shortlink(); ?>
[/php]

Expresate!

Para usar multiples lineas de código: <pre><code></code></pre>. Para publicar ejemplos usa CodePen, JsFiddle, Dabblet. No hagas SPAM, se amable y gracias por entender.