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

Last updated by Mariusz 5 years, 2 months ago.

Assigned support staff: Bruno.

Author Posts
July 17, 2014 at 1:10 pm #438061

Frank

Hello,

In this topic http://wpml.org/forums/topic/how-to-add-x-default/ (now marked as resolved), Denise suggests code to manually add a rel="alternate" hreflang="x-default" tag.

But when I copy-paste this code as indicated all hreflan tags become x-default as shown below

<meta name="generator" content="WPML ver:3.1.6 stt:1,4,3;0" />
<link rel="alternate" hreflang="x-default" href="<em><u>hidden link</u></em>" />
<link rel="alternate" hreflang="x-default" href="<em><u>hidden link</u></em>" />
<link rel="alternate" hreflang="x-default" href="<em><u>hidden link</u></em>" />

I did the copy-and-past several times so I'm quite sure that I did copy the exact code.

Are you 100% that there isn't any error in the code provided by Denise? Or what might be another problem?

Thanks for your help!

Frank

July 18, 2014 at 12:28 am #438311

Bruno
Supporter

Languages: English (English ) Portuguese (Brazil) (Português )

Timezone: America/Sao_Paulo (GMT-03:00)

Dear Frank,

Please, try this one:

		if ( $is_valid ) {
			foreach ( $languages as $code => $lang ) {
				if($code == $this->get_default_language()) {
					printf( '<link rel="alternate" hreflang="x-default" href="%s" />' . PHP_EOL, str_replace( '&', '&', $lang[ 'url' ] ) );
				} else {
					printf( '<link rel="alternate" hreflang="%s" href="%s" />' . PHP_EOL, $this->get_language_tag( $code ), str_replace( '&', '&', $lang[ 'url' ] ) );
				}
			}
		}

The problem is the other code on line:

if($code = $this->get_default_language()) {

You should have two "==" like this:

if($code == $this->get_default_language()) {

Please let me know if you are satisfied with my answer and if I can help you with any other questions you might have.

Thank you.

July 18, 2014 at 8:36 am #438470

Frank

Hello Bruno,

Your modification did resolve the problem that all hreflang tags become default, but the script doesn't do what it is expected to do.

Currently WPML produces the following code

<link rel="alternate" hreflang="fr" href="<em><u>hidden link</u></em>" />
<link rel="alternate" hreflang="en" href="<em><u>hidden link</u></em>" />
<link rel="alternate" hreflang="de" href="<em><u>hidden link</u></em>" />

The idea is to ADD another line to tell Google "For all users who neither use FR nor EN nor DE as language, show this version in the search results." For example, for users using Spanish language. So the new code should be the following (as I want by default the EN version to be shown in search results, as most international users understand English)

<link rel="alternate" hreflang="x-default" href="<em><u>hidden link</u></em>" />
<link rel="alternate" hreflang="fr" href="<em><u>hidden link</u></em>" />
<link rel="alternate" hreflang="en" href="<em><u>hidden link</u></em>" />
<link rel="alternate" hreflang="de" href="<em><u>hidden link</u></em>" />

But the code generated by the script is

<link rel="alternate" hreflang="x-default" href="<em><u>hidden link</u></em>" />
<link rel="alternate" hreflang="en" href="<em><u>hidden link</u></em>" />
<link rel="alternate" hreflang="de" href="<em><u>hidden link</u></em>" />

Hence the default language of the website (which is not necessarily that I want to show up in Google's search results) becomes the default version for search engines and the information what version should be shown to users with FR language is entirely lost.

Can you please modify the script so that x-default can be implemented as intended?

Thank you very much in advance!

Frank

July 18, 2014 at 10:59 pm #438932

Bruno
Supporter

Languages: English (English ) Portuguese (Brazil) (Português )

Timezone: America/Sao_Paulo (GMT-03:00)

Dear Frank,

Please, try this one:

		if ( $is_valid ) {

			printf( '<link rel="alternate" hreflang="x-default" href="<em><u>hidden link</u></em>" />' . PHP_EOL );
			
			foreach ( $languages as $code => $lang ) {
				printf( '<link rel="alternate" hreflang="%s" href="%s" />' . PHP_EOL, $this->get_language_tag( $code ), str_replace( '&', '&', $lang[ 'url' ] ) );
			}
		}

Please let me know if you are satisfied with my answer and if I can help you with any other questions you might have.

Thank you.

July 21, 2014 at 7:55 am #439452

Frank

Hello Bruno,

I hope you had a good weekend.

Unfortunately, this doesn't work because the script always inserts the same URL as x-default, as in

<link rel="alternate" hreflang="x-default" href="<em><u>hidden link</u></em>" />
<link rel="alternate" hreflang="fr" href="<em><u>hidden link</u></em>" />
<link rel="alternate" hreflang="en" href="<em><u>hidden link</u></em>" />
<link rel="alternate" hreflang="de" href="<em><u>hidden link</u></em>" />

The script would need to adapt dynamically the x-default version to the EN version.

Thank you for your continued help!

Frank

July 21, 2014 at 5:18 pm #439940

Bruno
Supporter

Languages: English (English ) Portuguese (Brazil) (Português )

Timezone: America/Sao_Paulo (GMT-03:00)

Dear Frank,

Please, try this one:

		if ( $is_valid ) {
			$langs = $lang_en = "";
			foreach ( $languages as $code => $lang ) {
				if( 'en' == $code ) {
					$lang_en = sprintf( '<link rel="alternate" hreflang="x-default" href="%s" />' . PHP_EOL, str_replace( '&', '&', $lang[ 'url' ] ) );
				}

				$langs.= sprintf( '<link rel="alternate" hreflang="%s" href="%s" />' . PHP_EOL, $this->get_language_tag( $code ), str_replace( '&', '&', $lang[ 'url' ] ) );
			}

			if( !empty( $langs ) ) {
				echo $lang_en;
				echo $langs;
			}
		}

Please let me know if you are satisfied with my answer and if I can help you with any other questions you might have.

Thank you.

July 22, 2014 at 8:01 am #440172

Frank

Ok. This seems to work. Thank you for your efforts!

It would be a lot easier if WPML offered a configuration through its WordPress dashboard.

When will such a feature become available?

July 22, 2014 at 9:55 pm #440689

Bruno
Supporter

Languages: English (English ) Portuguese (Brazil) (Português )

Timezone: America/Sao_Paulo (GMT-03:00)

Dear Frank,

I'm glad it has worked. I'll suggest it to our developers. Thanks for the suggestion.

Please let me know if you are satisfied with my answer and if I can help you with any other questions you might have.

Thank you.

July 23, 2014 at 6:10 am #440750

Frank

Hello Bruno,

I appreciate that you provided code to avoid the problem.

But frankly, 15 months after the first request to support x-default in WPML has been made (http://wpml.org/forums/topic/x-default-hreflang-when-to-expect-support-for-this-in-wpml/), I accepted a more advanced answer than "I'll suggest it to our developers".

Can you please ask the development team to provide an answer that gives the community at least an idea when to expect such a feature?

Thank you very much in advance!

Frank

July 23, 2014 at 2:59 pm #441223

Bruno
Supporter

Languages: English (English ) Portuguese (Brazil) (Português )

Timezone: America/Sao_Paulo (GMT-03:00)

Dear Frank,

I understand your frustration, but unfortunately I do not have a deadline as to when this will be added to the WPML core. Our developers have a to-do list and this is the list of tasks.

I sent a new message to them requesting it.

Thank you.

April 14, 2017 at 11:40 am #1254373

WP-Stars
/* ---------------------------------------------------------------------------
 * Set hreflang="x-default" according to Google content guidelines with WPML
 * Put into your functions.php - don't forget to use a child-theme ;-)
 * --------------------------------------------------------------------------- */
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 )) {
         
        echo '<link rel="alternate" href="' . $url . '" hreflang="x-default" />';
    }
     
    return $url;
}
October 13, 2017 at 12:47 pm #1400305

Mariusz

@WP-Stars it won't work slightly as you get a warning message:

Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'wps_head_hreflang_xdefault' not found or invalid function name in /var/www/wordpress/content/wp-includes/class-wp-hook.php on line 298