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

Last updated by Raja Mohammed 7 months, 3 weeks ago.

Assigned support staff: Raja Mohammed.

Author Posts
September 26, 2019 at 4:59 am #4643455


I need to add external links to hreflang sections to indicate Google my site in US has relation with the site on Canada.

Tell us what you are trying to do? I added the following code to hreflang section :
add_filter('wpml_alternate_hreflang', 'wps_head_hreflang_xdefault', 10, 2);
function wps_head_hreflang_xdefault($url, $lang_code) {

if($lang_code == apply_filters('wpml_default_language', NULL )) {

global $post;

echo '<link rel="alternate" href="' . $url . '" hreflang="x-default" />'.PHP_EOL;

/* External Links for Google SEO sake to let google know the relation betweens these 2 websites: USA and CA*/
echo '<link rel="alternate" href="hidden link'.$post_slug.'/?lang=fr" hreflang="fr-ca" />'.PHP_EOL;
echo '<link rel="alternate" href="hidden link'.$post_slug.'/" hreflang="en-ca" />'.PHP_EOL;


return $url;

This code works fine for me BUT
1- Im not sure if this is a correct way to do what im trying to achieve.
2- On products page, the slug is not correct on my external link(, it shows the slug of the first element it founds on the products page: *active-green-tea/* check attached picture

Is there any documentation that you are following? Im using that code i found on one of your post on forum

Is there a similar example that we can see? A sample is given in wpml forum:

What is the link to your site? hidden link

September 26, 2019 at 10:52 am #4645897

Raja Mohammed

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello there,
I will help you with the issue.

It seems like hidden link and hidden link are two different sites and your effort is to make Google understand the contents are related. Is my understanding is correct?

The filter wpml_alternate_hreflang will not have access to the global post variable. The best way I can think of is to parse the $url and replace the domain name with hidden link add the required parameters instead of fetching the post slug which is not available during the execution of the filter.

Alternatively, you can also replace the line

global $post;


$post_id = url_to_postid($url);

$post_slug = get_post_field( 'post_name', $post_id );

I hope that helps you better.

Kind regards

September 30, 2019 at 5:02 pm #4666705


Thank you Raja mohammed, the code you provided indeed helped me with my internal slug.

On the external links the code did not work. The external links still shows the first item it finds on Products page.
The same happen when pointing to the homepage of the external website. it shows:

but we do not have slug *home* is just


.pointing to the root.

example: [php]<link rel="alternate" href="hidden link" hreflang="en-ca" /></code

Hope you understand. thanks for your help

October 1, 2019 at 12:46 pm #4672085

Raja Mohammed

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I can say the code is working as expected. since you are adding slugs manually You can add some conditionals to check for the home slug and add or remove on a particular alternative link section.

I hope that should help.