WPML is an excellent plugin for managing multilingual sites as it allows to edit a lot of relevant info related to translated content. However, although this plugin adds hreflang tags, there's is a lack of support for hreflang="x-default" anotation.
So I would like to know how to add this x-default tag to WPML sites, and make it to point to my desired language version of a URL.
There are some examples of code in WPML forums, but they are intended for older versions of this plugin. All of them point to edit the head_langs function, that currently consists on the following code:
function head_langs()
{
$languages = $this->get_ls_languages( array( 'skip_missing' => true ) );
// If there are translations and is not paged content...
//Renders head alternate links only on certain conditions
$the_post = get_post();
$the_id = $the_post ? $the_post->ID : false;
$is_valid = count( $languages ) > 1 && !is_paged() && ( ( ( is_single() || is_page() ) && $the_id && get_post_status( $the_id ) == 'publish' ) || ( is_home() || is_front_page() || is_archive() ) );
if ( $is_valid ) {
foreach ( $languages as $code => $lang ) {
$alternate_hreflang = apply_filters( 'wpml_alternate_hreflang', $lang[ 'url' ], $code );
printf( '<link rel="alternate" hreflang="%s" href="%s" />' . PHP_EOL,
$this->get_language_tag( $code ),
str_replace( '&', '&', $alternate_hreflang ) );
}
}
}