生成多个goo.gl双语博客的快捷链接

时间:2012-05-26 作者:0x61696f

我正在用一个定制的主题修改我的个人博客,并在qtranslate的帮助下使其双语化。当涉及到自动生成短链接(出于分析原因提供)时,我已经停了下来。

到目前为止,我得到了以下代码,基于Kovshenin\'s goo.gl plugin, 查询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);
并在“我的帖子”面板中添加了一个漂亮的列,其中包含指向分析页面的链接:

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\');
问题是:上面的代码片段非常适合于单一语言环境,但当涉及双语时,它只生成默认语言的短链接。

问题是:我怎样才能让它也为第二语言生成链接?我认为很明显,我需要将它们保存在单独的元键中,但我找不到一种方法使其看起来比默认语言更远。

1 个回复
最合适的回答,由SO网友:0x61696f 整理而成

所以我设法解决了这个问题。这个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插件。

结束

相关推荐

列出分类法:如果分类法没有POST,就不要列出分类法--取决于定制的POST-META?

这可能很难解释,我不知道是否有解决办法!?我有一个名为“wr\\u event”的自定义帖子类型和一个名为“event\\u type”的分层自定义分类法。自定义帖子类型有一个元框,用于event_date 并且与此帖子类型关联的所有帖子都按以下方式排序event_date. 我在循环中有一个特殊的条件来查询event_date 已经发生了-在这种情况下,它没有显示,但只列在我的档案中。就像你可以使用wp_list_categories() 我编写了一个自定义函数,它以完全相同的方式列出所有分类术语。现在