• ResolvedModerator Marius L. J.

    (@clorith)


    Loving Polylang, it’s a very straight forward and simple tool to use so far, and for that I applaud you!

    I did notice, that you are catching calls to home_url() and that home_url( '/' ) (technically “no slug” provided) will provide you with a link that relates to the current active language, but as soon as you give it an actual slug, it will no longer provide the current language.

    A real world example, I have a site that has fixed buttons in a widget on the right hand side, the widget is hardcoded to use home_url( '/opening-hours/' ).
    Now if I visit mysite.com, this outputs mysite.com/opening-hours/ as expected, but if I access it with mysite.com/nb/ I would expect it to be mysite.com/nb/opening-hours/, but it ends up still being mysite.com/opening-hours/.

    https://www.ads-software.com/plugins/polylang/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Chouby

    (@chouby)

    Hi Marius,

    You are right home_url is filtered only for the home page. This is the only way to get the home page url in the correct language.

    For other types of content, we should generally use other functions such as get_permalink(), get_term_link(), get_author_link(), etc…

    These functions are filtered by Polylang, but in a way depending on the type of content:

    For links such as date archive, author archives, the current language code is added, so that you always get the archive in the current language.

    For links to posts or terms, the language code of the requested content is added, thus it may not be the current language and the link is not “translated”.

    home_url() is used in all sorts of places for urls which may need not to be filtered. I sometimes get support requests of users having issues with the language code wrongly added in the url, because a plugin/theme author used home_url() . '/someurl/' when I expect he would use home_url( '/someurl/' ).

    Moderator Marius L. J.

    (@clorith)

    Ahh I see, quite understandable.

    I created my own helper function to wrap your pll_home_url() call which gives me the expected base and then appends my own slugs like home_url() would and it works like a charm.

    Thank you very much for the response, and I do enjoy the plugin!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘home_url() always uses default language when providing a slug’ is closed to new replies.