So I’ve managed to solve this. The googl_shortlink
function from above now looks like this:
function googl_shortlink($url, $post_id = false) {
global $post;
if (!$post_id && $post) $post_id = $post->ID;
elseif ($post_id) $post = get_post($post_id);
// list all the active languages in an array
$enabled_languages = get_option('qtranslate_enabled_languages');
if (is_404())
return;
if ($post && $post->post_status != 'publish')
return "";
// go through each active language, get the properly formatted permalink,
// shorten it with goo.gl, then add it as a post_meta
foreach ($enabled_languages as $lid=>$lang) {
if ((is_singular() || $post) && !is_front_page() && qtrans_isAvailableIn($post_id,$lang)) {
$shortlink = get_post_meta($post_id, '_googl_'.$lang.'_shortlink', true);
if ($shortlink)
return $shortlink;
// the last parameter here was the major headache. It's called $forceadmin and defaults
// to false (by default it doesn't prepend language-specific path to links generate in /wp-admin)
$permalink = qtrans_convertURL(get_permalink($post_id),$lang,true);
$shortlink = googl_shorten($permalink); // shorten the url. No changes done here
if ($shortlink !== $url) {
add_post_meta($post_id, '_googl_'.$lang.'_shortlink', $shortlink, true);
} else {
add_post_meta($post_id, '_googl_'.$lang.'_shortlink', $url, true);
}
}
}
}
Trigger on save:
function googl_save_post($post_ID, $post) {
// Don't act on auto drafts.
if ($post->post_status == 'auto-draft')
return;
$enabled_languages = get_option('qtranslate_enabled_languages');
foreach ($enabled_languages as $lid=>$lang)
delete_post_meta($post_ID, '_googl_'.$lang.'_shortlink');
}
add_action('save_post', 'googl_save_post', 10, 2);
And show up in dashboard:
function googl_post_columns($columns) {
$columns['shortlink'] = 'Shortlink';
return $columns;
}
function googl_custom_columns($column) {
global $post;
if ('shortlink' == $column) {
$enabled_languages = get_option('qtranslate_enabled_languages');
foreach ($enabled_languages as $lid=>$lang) {
if (qtrans_isAvailableIn($post->ID,$lang)) {
$shorturl = get_post_meta($post->ID, '_googl_'.$lang.'_shortlink', true);
$shorturl_caption = str_replace('http://', '', $shorturl);
$shorturl_info = str_replace('goo.gl/', 'goo.gl/info/', $shorturl);
echo "$lang: <a href="https://wordpress.stackexchange.com/questions/53401/{$shorturl}">{$shorturl_caption}</a> (<a href="{$shorturl_info}">info</a>)<br />";
}
}
}
}
add_action('manage_posts_custom_column', 'googl_custom_columns');
add_filter('manage_edit-post_columns', 'googl_post_columns');
Drawbacks
I was unable to make wp_get_shortlink()
to work with the new shortlink schema. The above googl_shortlink
doesn’t return anything, since that would make the foreach loop bail out after processing the first language. Quick replacement (could be easily wrapped in a function to make it more friendly):
// can be used anywhere in the loop to get the
// shortlink for a specific post and language
get_post_meta($post->ID, '_googl_'.qtrans_getLanguage().'_shortlink', true);
Same thing goes for wp_shortlink_wp_head()
(since it calls on wp_get_shortlink):
function googl_shortlink_wp_head() {
global $post;
$shortlink = get_post_meta($post->ID, '_googl_'.qtrans_getLanguage().'_shortlink', true);
if (empty($shortlink))
return;
echo "<link rel="shortlink" href="".esc_url($shortlink)."" />\n";
}
// out with the old, in with the new
remove_action('wp_head','wp_shortlink_wp_head',10,0);
add_action('wp_head','googl_shortlink_wp_head',10,0);
Credits
Kudos goes to Konstantin Kovshenin for the simple, yet effective googl plugin.