WordPress: qtranslate: Clicking on the home link resets to the default language

I have a wordpress-based web site which is both in French and English. In order to be able to handle the two version easily I’m using the qtranslate plugin (a great plugin by the way !). 


Almost everything worked fine. The only issue I found was that if I clicked on my logo I was going to the appropriate home page but if I clicked on “Home” in the navigation menu, I was going to the home page in the default language.

I’ve setup qTranslate to have such URLs:

  • xxxx.com/fr/contact/ for the French contact page.
  • xxxx.com/en/contact/ for the English contact page.
  • Since French is the default language on this site, xxxx.com/contact is also taking you to the French contact page.


So basically you start on the French home page, click around and keep seeing the French version of the pages. Then change the language to English, click around and keep seeing the English version of the pages. But when you click on “Home”, instead of going to xxxx.com/en/, you go to xxxx.com/fr/ which then switches you back to the French site.

After a lot of googling, code reading and trying various stuff, I finally figured out a solution:

qTranslates installs a filter for all wordpress hooks related to displaying posts/pages or navigating to make sure that you see the right page. But it looks like no filter is installed for ‘home_url’. Just adding it solved the problem.

In order to fix it, go to Plugins > Editor. Choose qTranslate. Then open the file qtranslate/qtranslate_hooks.php. Scroll down. At the end of the file, you’ll see a bunch of add_filter(…); calls. Add there the following:

add_filter('home_url', 'qtrans_convertURL');

Save the file and enjoy clicking on the Home menu item without having the language reset to default !

26 thoughts on “WordPress: qtranslate: Clicking on the home link resets to the default language

  1. It sort of works, as it the makes the links correct, however it then doesnt load the posts properly and instead gives:

    Not Found, Error 404
    The page you are looking for no longer exists.

    1. That’s strange. I didn’t notice any issue on the web site I used it on (ams-traduction.com). But there I only use pages and no post. How does the post link URL look like for which you get the 404 error ?

      1. The fix gives the same problem for me too.
        All links get fixed (including home :D) but the content itself doesn’t work (Page not found error). So it seems that the internal linking system gets broken with this fix.
        Also I don’t use posts but just pages. Any ideas?

        Thanks in advance,

        NJ

  2. Awesome tip, thank you so much ! I was searching on how to also translate wp links but i’m glad i found this too 🙂

    1. I’ve created a post to test it and even there with the fix it doesn’t reset to the default language when clicking on the home link. Do you have site where I could have look at this problem ?

  3. Thanks friend, you just saved me a whole lot of trouble. For some reason `qtrans_convertURL` did nothing when I used it to surround the code that generates the menu links in the template but I don’t mind because it works now. I’ll write a post on my site about all the problems I had with qTranslate and how they were solved (for the benefit of the web) and I’m bookmarking this page to use as a reference.

    Thanks a million!

  4. Solved by injecting some jQuery. You’ll need a qtrans language chooser on the pages and this code will need to be adapted on your qtrans path mode. this is the easiest, fastest and more reliable way I came up to solve this problem … till now 🙂

    jQuery(document).ready( function(event) {
    // solve menu home wrong qtranslate link
    var lang = jQuery(‘.qtrans_language_chooser .active a’).attr(‘hreflang’);
    jQuery(‘.menu-item-home>a’).attr(‘href’, jQuery(‘.menu-item-home>a’).attr(‘href’)+lang )
    });

  5. This solution only works for built-in menu. Great plugin unable to find correct URL when changing language? Look at Google: 1400 request for solution, no one works. Mediocre.

    1. I know it is frustrating when it does not work as it should. If the plugin author does not fix it himself, since the code is available you could fix it and provide the fix to the community and ask the plugin author to take it over in the next version.

  6. Glad I found this thread. I’ve spent the last 3 hours researching, and I feel like I’m very close to finding the right way to do this, perhaps you guys can help.

    I use qTranslate + qTranslate slug plugin. My site can toggle between french & english.

    Links appear like this: mysite.com/fr/pagetitle mysite.com/en/pagetitle

    I did manage to get French/English text labels to be integrated in my main nav menu… the only problem is that it just reverts back to the mysite.com/fr or mysite.com/en (basically the homepage) rather than just switching the current content to the proper language.

    What code should I add so I don’t redirect users to the french/english homepage but rather switch language on the current page they are visiting?

    Thanks a bunch!

  7. Hi all. I have a similar problem. i have custom link in top menu
    /?s=~&tfseekfid=main_search&stype=1 and qtranslate not adding ?lang=es

  8. Can somebody help me? I have the same issue that when “Clicking on the home link resets to the default language”

    I’m Using
    WordPress 3.9.2
    QTranslate Version 2.5.40b

    ans still not working…

  9. It goes perfect to me!!!
    I use qTranslate Plus and instead of:
    add_filter(‘home_url’, ‘qtrans_convertURL’);
    I put:
    add_filter(‘home_url’, ‘ppqtrans_convertURL’);
    and… PERFECT!
    Thanks a lot!

Leave a Reply

Your email address will not be published. Required fields are marked *