1

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.

1 Answer 1

1

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='{$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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.