Choose the x-default Language for WPML hreflang Tags
Points WPML's x-default hreflang tag at the language you choose, for example English, instead of the site's default language.
Opens WPIDE on
Code
$wpide_x_default = 'en'; // hreflang code to use as x-default.
add_filter( 'wpml_hreflangs', function ( $hreflangs ) use ( $wpide_x_default ) {
if ( is_array( $hreflangs ) && ! empty( $hreflangs[ $wpide_x_default ] ) ) {
$hreflangs['x-default'] = $hreflangs[ $wpide_x_default ];
}
return $hreflangs;
} );
Notes
Use the hreflang code as WPML prints it (en, en-us, fr…). If that language has no translation of the page, the tag is left as WPML set it.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.