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.

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

Problem:
CORS not enabled
Solution:
We have allowed origins as follows:

// wp-config.php

if (isset($_SERVER["HTTP_ORIGIN"]) === true) {
	$origin = $_SERVER["HTTP_ORIGIN"];

        // $allowed_origins is an array of domains to be allowed; it could as well be filled by all active languages urls from WPML
	$allowed_origins = array(
					"https://www.domain1.com",
					"https://www.domain2.com"
	);
	if (in_array($origin, $allowed_origins, true) === true) {
					header('Access-Control-Allow-Origin: ' . $origin);
					header('Access-Control-Allow-Credentials: true');
					header('Access-Control-Allow-Methods: POST');
					header('Access-Control-Allow-Headers: Content-Type');
	}
	if ($_SERVER["REQUEST_METHOD"] === "OPTIONS") {
					exit; // OPTIONS request wants only the policy, we can stop here
	}
}

In our setup, we were loading posts via REST endpoints and had problems using

get_rest_url()

It always returned the REST endpoint based on main domain's home URL.

Lucky enough:

site_url()

has returned the correct home URL.

So to get our REST endpoint right we used:

$rest_url = site_url() . '/wp-json/';

This topic contains 3 replies, has 3 voices.

Last updated by Bruno Kos 3 weeks, 5 days ago.

Assigned support staff: Bruno Kos.

Author Posts
September 18, 2019 at 3:27 pm #4598127

enrikB

I am trying to: Create a multilingual site with two different domains. One for German and one for French. When going to the backend of the translated page in the secondary language the editor throws errors.

I expected to see: The editor loading normally

Instead, I got: Empty blocks

September 19, 2019 at 12:18 pm #4604633

Otto
Supporter

Languages: English (English ) Spanish (Español )

Timezone: America/Argentina/Buenos_Aires (GMT-03:00)

Hi there,

Please apologize for the delay in answering. This is not usual in this forum. While one of my teammates takes care of your case I will provide you with the first debugging steps.

It is a server issue, you need to enable CORS in order to allow the JS resources to load:
hidden link

Kind regards,

Otto

September 20, 2019 at 7:51 am #4610231

enrikB

Hi Otto

Thank you for your feedback.
In the meantime we were able to resolve the problem.

For reference purposes, I'm adding our solution below.

We have allowed origins as follows:

// wp-config.php

if (isset($_SERVER["HTTP_ORIGIN"]) === true) {
	$origin = $_SERVER["HTTP_ORIGIN"];

        // $allowed_origins is an array of domains to be allowed; it could as well be filled by all active languages urls from WPML
	$allowed_origins = array(
					"<em><u>hidden link</u></em>",
					"<em><u>hidden link</u></em>"
	);
	if (in_array($origin, $allowed_origins, true) === true) {
					header('Access-Control-Allow-Origin: ' . $origin);
					header('Access-Control-Allow-Credentials: true');
					header('Access-Control-Allow-Methods: POST');
					header('Access-Control-Allow-Headers: Content-Type');
	}
	if ($_SERVER["REQUEST_METHOD"] === "OPTIONS") {
					exit; // OPTIONS request wants only the policy, we can stop here
	}
}

In our setup, we were loading posts via REST endpoints and had problems using

get_rest_url()

It always returned the REST endpoint based on main domain's home URL.

Lucky enough:

site_url()

has returned the correct home URL.

So to get our REST endpoint right we used:

$rest_url = site_url() . '/wp-json/';

I know there are probably better ways to do this, but we needed a quick fix and this helped us.
If you, your colleague or anyone else comes up with a proper solution, we'd love to hear it.

Best Regards

September 20, 2019 at 9:09 am #4610851

Bruno Kos
Supporter

Languages: English (English )

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

Hi,

I think that this is quite rare scenario - however, thank you very much for providing this solution. I posted it as a summary this thread, hopefully other clients will benefit from it in case they encounter the same issue.

Regards,
Bruno Kos