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 14 replies, has 3 voices.

Last updated by Marcel 1 month, 2 weeks ago.

Assigned support staff: Marcel.

Author Posts
August 15, 2019 at 7:34 am

adrianS-30

I'm trying to get the correct translated url by using the filter 'wpml_permalink'.

Please check the code lines 15-29 in the /httpdocs/wp-content/themes/twentynineteen/functions.php.

For this same code I get different results on the live and 100% cloned staging server, which already doesn't make any sense, but the worst is that neither of both results are correct...

I want to translate full correct urls:
url.com/en/about/ to url.com/de/uber/
url.com/de/uber/ to url.com/en/about/

Please open both of the pages url.com/en/about/ and url.com/de/uber manually and see that the code also gives different results depending on the current language.
The result is also different if the code is called on url.com/en/about and url.com/en/hello-world/.

The results are unpredictable, what's going on here?

I just want to get the exact same link which is generated for the language switcher in the footer. Is there an easy way to achieve what I want?

Thanks.

August 15, 2019 at 12:48 pm #4400837

Lauren
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

Thank you for contacting WPML support. I'll be happy to help you with this. I'm not sure I'm understanding the issue. It looks like you are trying to use the German directory /de/ for the English page slug "about" and the English directory /en/ for the German page slug "uber". If this is the expected outcome, you can simply change the page slug in the page edit screen. You can also set the slugs to be translatable in WPML -> Settings.

Perhaps I am not understanding the goal, and if this is the case, please outline exactly what it is you would like to do. Thanks in advance for clarification.

August 16, 2019 at 2:49 am #4404233

adrianS-30

I use /en/ for English and /de/ for German pages.
I also use translated page slugs in the URL.
No issues here, both url.com/en/about/ and url.com/de/uber exist and work.

What I want to achieve is redirecting visitors from one language to the other via PHP, if the current page language is not their browser language.
The redirecting is no problem, but I struggle to get the English URL from the German URL and vice versa. So in case the English visitor is on the German About page I want to redirect him from url.com/de/uber/ to url.com/en/about/.

It looks like wpml_permalink with $full_resolution set to true is meant to do that, see https://wpml.org/wpml-hook/wpml_permalink/. But as you can see in the code output it doesn't seem to work correctly and consistently.

Issue 1: Open both url.com/en/about/ and url.com/de/uber in different browser tabs and see that the same code generates different outputs (see cell first row third column).

Issue 2: In rows 3 and 4 the transformations from the German to the English URL doesn't work, no matter if $full_resolution is null or true.

Issue 3: In row 2 the transformation from the English to German URL works like expected. On my live website it doesn't. On the live website I get url.com/en/about/ instead of url.com/de/uber/ in the cell second row third column. I have no clue why this happens and can't replicate it on the staging website.

Hope you see the problems now and hopefully the fix of the staging website's issues 1 & 2 also solve the live website's issue 3.

Thank you!

August 19, 2019 at 1:10 pm #4416273

Lauren
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

Thanks for the additional information. Does the live website have any caching? Is there any difference if you test with hardcoded URLs instead of http_host?

Also, in regards to the different results, testing in two tabs won't work because languages are stored as cookies and the last opened tab should display the actual results.

Lastly, I've set up a clean install so that we can try to reproduce issue 2 to see if this is a bug. Please log in here and add your code so we can test it in a fresh install environment., and let me know if you get the same results as your live site.

Login: hidden link
Username: demo
Password: doEIK97Tu3q3

August 20, 2019 at 3:04 am #4420199

adrianS-30

No the website doesn't have caching and there's no difference with hardcoded URLs, but I hardcoded them now.

Regarding issue 1: I don't understand what this has to do with cookies, since the function should always give the same result for the same input, shouldn't it? Can you explain this to me please and also why the same function input gives different output on /en/about/ and /de/ueber/?

Regarding issue 2:
- I configured WPML on your environment
- I changed the language URL format to "Different languages in directories", checked "Use directory for default language" and re-saved permalinks
- I created English and German About pages
- I created English and German primary menus for the About pages
Result:
- when $full_resolution is set to true, it actually generates the correct results and I don't understand this, since my environment was also fresh and I did the exact same steps
- /en/about/ and /de/ueber/ still generate different outputs when $full_resolution is null

I'm really confused right now and wonder why every environment gave different function outputs for the same input so far.

August 21, 2019 at 12:18 pm #4431159

Lauren
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

We found that there was a typo in the code, please see attached screenshot. It is now working as it should here: hidden link

Please let me know if you need any further assistance and I will be happy to help.

August 22, 2019 at 2:40 am #4435727

adrianS-30

Oops, made this mistake because WordPress sometimes makes an "u" and sometimes an "ue" permalink out of an "ü". But this typo only occurred in the 2 replicated environments. In my live instance the typo doesn't exist and it still doesn't work.

I was able to clone the live instance with the issue. Deleted all plugins and themes, so there's no way that any customisations from my side break the functionality, but it still doesn't work (e.g. see the transformation from the English to the German URL in the second row).

hidden link
hidden link
hidden link

Login credentials for web and FTP are the same, only the the FTP username is different: clonewpml

Thanks!

August 22, 2019 at 7:36 pm #4442521

Lauren
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

Thanks for setting up that cloned site and allowing us access. I have escalated this to our 2nd tier support team for further investigation. We will update here as soon as we have more information.

August 29, 2019 at 4:11 pm #4482571

Lauren
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

Our developers found that the issue is happening when display as translated is enabled in WPML -> SEttings. They have found a workaround. Please try changing the code in wp-content/plugins/sitepress-multilingual-cms/classes/url-handling/wpml-url-filters.class.php to:

	public function permalink_filter( $link, $post ) {
		if ( ! $post ) {
			return $link;
		}

		$post_id = $post;

		if ( is_object( $post ) ) {
			$post_id = $post->ID;
		}

		/** @var int $post_id */
		if ( $post_id < 1 ) {
			return $link;
		}

		$canonical_url = $this->canonicals->permalink_filter( $link, $post_id );
		if ( $canonical_url ) {
			return $canonical_url;
		}

		$post_element = new WPML_Post_Element( $post_id, $this->sitepress );
		$trid = apply_filters( 'wpml_element_trid', NULL,$post_id );
		$translations = apply_filters( 'wpml_get_element_translations', NULL, $trid );
if ((count($translations) > 1) || ( ! $this->is_display_as_translated_mode( $post_element ) &&
		                     $post_element->is_translatable() )) {
			$link = $this->get_translated_permalink( $link, $post_id, $post_element );
		}

		return $this->url_converter->get_strategy()->fix_trailingslashit( $link );
	}

Please let me know if this resolves the issue for you.

August 30, 2019 at 11:27 am #4486837

adrianS-30

I see that the issue is fixed on the clone site. But by only replacing the code of the function the issue is not solved on my live site. What else did you do? What do you mean with 'display as translated'? I don't find something like that in WPML -> Settings.
Thanks!

August 30, 2019 at 1:56 pm #4487577

Lauren
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

The setting is on WPML -> Settings in the post type section. Are your pages set to "Translate Only" or "Translate or Fallback to default"? The Translate or fallback to default is the setting that is referred to as "display as translated". Does this setting on your live site match the setting on your test site?

August 31, 2019 at 8:00 am #4490423

adrianS-30

On the clone site it was set to "Translate Only" for pages, on my live site it was "Translate or Fallback to default". I set it now to "Translate Only", but the issue is still not solved.
So you / the developers must have changed more than that.

Thanks!

September 2, 2019 at 4:37 pm #4499199

Marcel
Supporter

Languages: English (English ) German (Deutsch )

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

Hi Adrian,

my name is Marcel, and I'm taking over this ticket as Lauren is not available today.

I checked your site and saw that some PHP lines from Lauren's workaround were missing on your site (parts from lines 184+). I added the full workaround again. Could you please recheck the site?

Best Marcel

(You can reply to me also in German if you prefer)

September 4, 2019 at 10:26 am #4510047

adrianS-30

Hi Marcel,

I only replaced the function's code on my live instance.

I think you are talking about hidden link and replaced the code there right? This is the wrong domain where this issue did not exist. Lauren and I were dealing with hidden link as you can see in the history of this thread.

I just checked the permalink_filter function code on hidden link and wonder now that the issue is fixed there even though the function code was not replaced by the code Lauren posted here? What did you change to fix the issue? Before I gave you access the issue on hidden link was exactly the same as on my live instance.

Thanks!

September 4, 2019 at 11:08 am #4510373

Marcel
Supporter

Languages: English (English ) German (Deutsch )

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

Hi Adrian,

yes, that's right. I was on the wpml. subdomain, as the credentials were attached to them.

I logged in now to clone. and can confirm the change is not active here. As I didn't access this subdomain before your latest reply there was nothing I changed, but I'm happy to hear that it works now.

Best Marcel