Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using qTranslate for multi-language & wp-commentnavi for comment navigation. The style of my base permalinks are like so:

site.com/     -> for default language
site.com/en/  -> for other language

I need help in regards to the following as they keep redirecting to the default language:

1. Comment submission

How do I hack this? I want the current language to be displayed after submission.

I found this proposed solution:

http://www.qianqin.de/qtranslate/forum/viewtopic.php?f=3&t=2631&p=9037#p8759

..but the problem with that is that it doesn't direct to the right comment page if there's pagination for the comments, ie. it directs to the first comment page instead of the last page where the new comment is.

2. Next/Prev comment page links

It seems that wp-commentnavi uses next_comments_link() & previous_comments_link() for the Next/Previous comment page links respectively. For the rest of the links (eg. pages, first, & last) I can use the qtrans_convertURL() function to direct them to the current language. But I don't know how I could hard code the links for the two former ones, ie. next/previous. Anyone knows?

3. Comment Link

I have a link in each comment to the comment link using get_comment_link(). Something like so:

qtrans_convertURL( esc_url( get_comment_link($comment->comment_ID) ) )

For comment pages two & above, the links work fine, in that they direct to the correct language. But for links that are on page 1, although the permalinks are correct, but once opened they get redirected to the default language.

For example, in page 1, one comment have a link like so:

site.com/en/post-type/post-name/comment-page-1/#comment-2

When opened, it redirects to:

site.com/post-type/post-name/#comment-2

It seems that anything with comment-page-1 is redirected to a shorter permalink, ie. it got rid of the comment page permalink, but the problem is that it also got rid of the language permalink.

How do I maintain the current language?

Anyone could help? Cheers in advance! :)

share|improve this question

1 Answer 1

up vote 0 down vote accepted

I've figured out the solution to my problems. Here's what I did...

Note:
This is considering the posts, thus comments, are under the 'Article' post-type, which thus create the permalink such as this: site.com/article/post-name/#comments.
Adjustments should be made for other uses.

To fix problem #1 & #3:

if ( !is_admin() )
    add_filter('get_comment_link', 'my_comment_post_redirect');

function my_comment_post_redirect($location){

    // Retrieve the URL based on current language up to the post-type ('article')
    $currUrl = preg_replace('/(.*article\/).*/', '$1', $_SERVER["HTTP_REFERER"]);

    // Retrieve the URL from the post name to the location of the comment
    // (ie. comment page & number/ID)
    $cmntPage = preg_replace('/.*article\/(.*)/', '$1', $location);

    // Comment page #1 has special redirection, thus...
    $cmntPageNum    = preg_replace('/.*comment-page-(.*)\/.*/', '$1', $cmntPage);
    // If on first comment page...
    if ($cmntPageNum == 1) {
        // ...then get rid of comment page from permalink
        $cmntPage = preg_replace('/(.*)comment-page-.*\/(.*)/', '$1$2', $cmntPage);
    }

    return $currUrl.$cmntPage;
}

UPDATED: The above code will also affect comment links in the back-end so I added if ( !is_admin() ) at the top to prevent it from happening.

For problem #2, I created a function for each link (next & previous):

function def_get_next_comments_link($label, $max) {
    // Get the complete code generated by the function
    // (which includes the <a> tag)
    $navLink = get_next_comments_link($label, $max);
    // Retrieve the base URL up to the post-type 'article'
    $baseUrl = preg_replace('/.*(http:.*article\/).*/', '$1', $navLink);
    // Replace the base URL with the one for the current language
    return str_replace($baseUrl, qtrans_convertURL($baseUrl), $navLink);
}

function def_get_previous_comments_link($label) {
    // Get the complete code generated by the function
    // (which includes the <a> tag)
    $navLink = get_previous_comments_link($label);
    // Retrieve the base URL up to the post-type 'article'
    $baseUrl = preg_replace('/.*(http:.*article\/).*/', '$1', $navLink);
    // Replace the base URL with the one for the current language
    return str_replace($baseUrl, qtrans_convertURL($baseUrl), $navLink);
}

Then I just called the functions where the links are.

This worked for me. :)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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