I'm working on revamping my personal blog with a custom built theme and making it bilingual with the help of qtranslate. I've gotten to a grinding halt when it comes to automatically generating the shortlinks (offered for analytics reasons).
Up till now, I got the following code, based on Kovshenin's goo.gl plugin, that queries the google shorten API:
function googl_shortlink($url, $post_id) {
global $post;
if (is_404())
return;
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);
and adds a nice column to my posts dashboard with a link to the analytics page:
function googl_post_columns($columns) {
$columns['shortlink'] = 'Shortlink';
return $columns;
}
function googl_custom_columns($column) {
global $post;
if ('shortlink' == $column) {
$shorturl = wp_get_shortlink();
$shorturl_caption = str_replace('http://', '', $shorturl);
$shorturl_info = str_replace('goo.gl/', 'goo.gl/info/', $shorturl);
echo "<a href='{$shorturl}'>{$shorturl_caption}</a> (<a href='{$shorturl_info}'>info</a>)";
}
}
add_action('manage_posts_custom_column', 'googl_custom_columns');
add_filter('manage_edit-post_columns', 'googl_post_columns');
Here's the problem: The snippet above works perfectly for a single language environment, but when it comes to going bilingual, it only generates the shortlinks for the default language.
The question is: How can I make it generate the links for the second language as well? I think it's obvious that I'll need to save them in separate meta keys, but I can't find a way to make it look further than the default language.