• Resolved Iudex

    (@iudex)


    I’m having some problems using TranslatePress together with another plugin that uses the REST API. The other plugin works just fine for default language (with normal links), but since TranslatePress adds the language slug ie. ‘en’ to the URL I get “unauthorized” in return. It only works if I am logged in as admin.

    Is there a way to turn off force language in links when using REST API? I just need my WordPress to use /wp-json/plugin-name/* all the time instead of /en/wp-json/plugin-name/*.

    • This topic was modified 6 months, 3 weeks ago by Iudex.
    • This topic was modified 6 months, 3 weeks ago by Iudex.
Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Cristian Antohe

    (@sareiodata)

    Hello,

    You can exclude the path in TranslatePress -> Settings -> Advanced -> Exclude strings -> Do not translate certain paths

    It should stop TP from translating and adding the language parameter in a secondary language.

    Best Regards

    Thread Starter Iudex

    (@iudex)

    Thanks for the quick reply. I already tried setting all kinds of variations of the URL that is used for the REST API namespace without any luck. Even the page that calls the REST API. But even if I already added all kinds of variations, shouldn’t /wp-json/* be enough?

    Unfortunately neither works. Somehow it seems that it doesn’t apply for API calls. I still get an “Unauthorized” error in the console, with the link to the API with the language code in the URL (/en/wp-json/….).

    • This reply was modified 6 months, 3 weeks ago by Iudex.
    Plugin Author Cristian Antohe

    (@sareiodata)

    If you need to access the non /en/ version, simply don’t call it.

    Is the plugin that’s trying to access it from your own website? And the error is in the browser console?

    Depending on how the plugin is generating the link to the json api, it might or might not be fixable.

    Can you please give us a bit more information?

    Plugin Author Cristian Antohe

    (@sareiodata)

    For example, on secondary languages, the home_url get filltered in order to receive the language parameter.
    The site_url however does not receive the site URL. So this means you can use site_url to construct a URL without the language parameter.

    Thread Starter Iudex

    (@iudex)

    The plugin is using the get_rest_url() function, and when TranslatePress is used this URL is returned with a language code and therefore returning a link that doesn’t work.

    Thread Starter Iudex

    (@iudex)

    The error is displayed in the console, yes. I have checked what get_rest_url() returns and can confirm that TranslatePress adds the language code as the slug in the returned URL. This will make plugins using get_rest_url() unsuable. And I find no way to disable it in TranslatePress. Already tried excluding the API URLs in TranslatePress, but doesn’t seem to work for that function.

    Plugin Author Cristian Antohe

    (@sareiodata)

    Hi,

    Can you try out this code to remove the lang param from the rest api?

    /*
    * Remove language from plugin rest api.
    */
    add_filter( ‘trp_home_url’, ‘trp_home_url_no_lang_for_rest_api’, 10, 5);
    function trp_home_url_no_lang_for_rest_api( $new_url, $abs_home, $TRP_LANGUAGE, $path, $url ){
    if (str_contains($path, ‘wp-json/plugin-name’)){
    return $url;
    }

    return $new_url;
    }

    Thread Starter Iudex

    (@iudex)

    Seems like this is working fine!

    Thread Starter Iudex

    (@iudex)

    @sareiodata Seems I was a bit too quick in my reply. Not resolved. Same error.

    • This reply was modified 4 months, 3 weeks ago by Iudex.
Viewing 9 replies - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.