所以我设法解决了这个问题。这个googl_shortlink
上面的函数现在如下所示:
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);
}
}
}
}
保存时触发:
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);
并显示在仪表板中:
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=\'{$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\');
缺点我无法
wp_get_shortlink()
使用新的短链接架构。以上
googl_shortlink
不返回任何内容,因为这将使foreach循环在处理第一语言后退出。快速更换(可轻松包装在功能中,使其更加友好):
// 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);
同样的道理也适用于
wp_shortlink_wp_head()
(因为它调用wp\\u get\\u 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);
荣誉归于
Konstantin Kovshenin 对于简单但有效的googl插件。