Polylang hreflang: Set Which Language Is x-default
Adds or replaces the x-default hreflang link Polylang prints, pointing it at the language you choose so search engines know which version to show everyone else.
Opens WPIDE on
Code
$wpide_x_default = 'en'; // hreflang code to use as x-default.
add_filter( 'pll_rel_hreflang_attributes', 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 Polylang prints for that language (often en or en-US; view your page source to check).
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.