Skip Navigation

This thread is resolved. Here is a description of the problem and solution.

Problem:
You are developing a site and want the WordPress API to respond in English, despite the main website language being different. You tried using the WPML Hook with

do_action('wpml_switch_language', 'en');

but it did not work.
Solution:
We recommend using the

rest_request_before_callbacks

filter to force the WPML language for specific API endpoints. Here is an example of how you can implement this:

add_filter('rest_request_before_callbacks', function ($response, $handler, $request) {<br />    // Specify the endpoints you want to target (or apply globally).<br />    $target_endpoints = ['/wp/v2/posts', '/wp/v2/pages'];<br />    <br />    if (in_array($request->get_route(), $target_endpoints, true)) {<br />        // Switch WPML language context to English.<br />        do_action('wpml_switch_language', 'en');<br />    }<br />    <br />    return $response;<br />}, 10, 3);

Please note that we do not provide support for custom coding issues, but only guidance and examples as shown above. If you need further assistance, you might consider hiring a WPML Contractor.

This solution might be irrelevant if it's outdated or not applicable to your case. We highly recommend checking related known issues at https://wpml.org/known-issues/, verifying the version of the permanent fix, and confirming that you have installed the latest versions of themes and plugins. If this does not resolve your issue, please open a new support ticket.

This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

Tagged: 

This topic contains 1 reply, has 0 voices.

Last updated by Dražen 1 week, 6 days ago.

Assisted by: Dražen.

Author Posts
January 17, 2025 at 1:45 pm #16606354

kaiE-2
Site owner

Background of the issue:
I am developing a site and want the WordPress API to respond in English. The main website language is another language, and I want to force all or some API endpoints to respond in English. I tried using the WPML Hook: do_action('wpml_switch_language', 'en'); but without success. For the WP Hook, we used: add_filter('rest_request_before_callbacks', ....).

Symptoms:
The WPML Hook used did not successfully make the WordPress API respond in English.

Questions:
How can I achieve that the API responds in English?

January 21, 2025 at 8:38 am #16616798

Dražen
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+01:00)

Hello,

thanks for contacting us. I am not sure what approach you use but might be an issue with your custom code.

You can use the rest_request_before_callbacks filter to force the WPML language for specific API endpoints. I have just tried and seems to work fine, for example:

add_filter('rest_request_before_callbacks', function ($response, $handler, $request) {
    // Specify the endpoints you want to target (or apply globally).
    $target_endpoints = ['/wp/v2/posts', '/wp/v2/pages'];
    
    if (in_array($request->get_route(), $target_endpoints, true)) {
        // Switch WPML language context to English.
        do_action('wpml_switch_language', 'en');
    }
    
    return $response;
}, 10, 3);

I hope this helps, but note we can not help with custom coding issues, but only provide guidance and examples like I did above. If you need further help you can hire WPML Contractor: https://wpml.org/contractors/

Regards,
Drazen