Skip Navigation

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

Problem:
The client wants to add a trailing slash to the hreflang URL output in the by WPML to avoid SEO problems due to redirects.
Solution:
We recommend a workaround that involves editing the WPML plugin files directly. Before proceeding, ensure to back up your site. The file to modify is

wp-content/plugins/sitepress-multilingual-cms/sitepress.class.php

.
Find the

language_url

method and replace the existing code with the following:

function language_url( $code = null ) {
	global $wpml_url_converter;

	if ( is_null( $code ) ) {
		$code = $this->this_lang;
	}

	$abs_home = trailingslashit($wpml_url_converter->get_abs_home());
	$url      = $this->convert_url( $abs_home, $code );

	return $url;
}

Our developers are aware of this issue and are working on a fix that may be included in a future update, possibly WPML 4.6.9, but there is no guarantee. Implementing the above code change will not affect your site's functionality, but be aware that updating the plugin to a newer version will overwrite this change unless the fix is included in that update. Currently, there is no hook/filter available for this adjustment in your theme's functions.php.

If this solution doesn't look relevant to your situation, please open a new support ticket with us.

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

Last updated by Mihai Apetrei 1 year, 5 months ago.

Assisted by: Mihai Apetrei.

Author Posts
December 14, 2023 at 4:20 pm #15066519

marcW-15

I would like to add a trailing slash to the hreflang url that is output to the <head> by WPML.
What WPML outputs: <link rel="alternate" hreflang="de" href="hidden link"/>
What I want: <link rel="alternate" hreflang="de" href="hidden link"/>

Why do I want that? The whole site gets redirected to the domain name with the trailing slash (hidden link), so what happens is that the link gets redirected resulting in an SEO problem. It would be better if it were there in the first place.

In the documentation for the feature „languages in directories“, there is no mention of an option to add or remove a trailing slash. https://wpml.org/documentation/getting-started-guide/language-setup/language-url-options/

December 15, 2023 at 12:17 pm #15072107

George Botsev
WPML Supporter since 02/2014

Languages: English (English )

Timezone: Europe/Sofia (GMT+03:00)

Hello and thank you for contacting us.

If I have understood you correctly here, the changes that you want to implement are to the default language URL and you want it to be slashed for the hreflang entries.

We already are aware of such a problem and I have a workaround that you could potentially try - do remember to make backup first.

in wp-content/plugins/sitepress-multilingual-cms/sitepress.class.php - the method: \SitePress::language_url
change from:

	function language_url( $code = null ) {
		global $wpml_url_converter;

		if ( is_null( $code ) ) {
			$code = $this->this_lang;
		}

		$abs_home = $wpml_url_converter->get_abs_home();
		$url      = $this->convert_url( $abs_home, $code );

		return $url;
	}

change to:

	function language_url( $code = null ) {
		global $wpml_url_converter;

		if ( is_null( $code ) ) {
			$code = $this->this_lang;
		}

		$abs_home = trailingslashit($wpml_url_converter->get_abs_home());
		$url      = $this->convert_url( $abs_home, $code );

		return $url;
	}
December 18, 2023 at 2:36 pm #15085185

marcW-15

Hello George,

thank your for your reply!

While your code does look like it may work, I am hesitant to modify WPML plugin files directly.
I would much prefer using a hook / filter in my theme's functions.php.

Are you aware of any plans to implement such a filter?

December 18, 2023 at 2:49 pm #15085341

Mihai Apetrei
WPML Supporter since 03/2018

Languages: English (English )

Timezone: Europe/Bucharest (GMT+03:00)

Hi there.

Our developers are already aware of this issue, as my colleague confirmed above, and there is already an internal ticket where they are working on fixing this and deploying the fix in a future update.

This fix might come in WPML 4.6.9 but I can't guarantee it. When exactly our developers will release an update including the required feature is not up to us, the Supporters, to determine.

Each issue reported is taken seriously into consideration. It is given a high or low priority based on several factors, factors which those with the overview of all things related are in the best position to determine.

What I can guarantee for now is that implementing this change in the code that was recommended above won't affect the site or its functionality in any way and you are already using the latest version that we have available.

The only thing to keep in mind is that, if you update it to a newer version (when one becomes available), the old code will be overwritten so you will have to add it again in the code, in case the fix is not coming with that plugin version, too.

Unfortunately, I don't know of any plans to have this code rewritten so that it could be used as a hook/filter in your theme.

The workaround shared above by my colleague is the only one existing at the moment and when there's an issue, usually that's what our team does: they create a workaround like this one, that can be added to our plugin's code and that fixes the issue until they release a fix in an upcoming version.

I hope that you will find this information helpful.

Mihai

December 18, 2023 at 3:02 pm #15085403

marcW-15

Hello Mihai,

thank you for sharing this information! All considered, I think in my case it is preferable to wait for the implementation of a hook to resolve my issue.