0

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( '&amp;', '&', $alternate_hreflang ) );
            }
        }
    }
Dharman
  • 30,962
  • 25
  • 85
  • 135

2 Answers2

0
/* ---------------------------------------------------------------------------
 * 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;
}
  • I have two languages in my website and adding this function is adding new alternate link instead of replacing the default language hreflang. – edonbajrami Feb 26 '19 at 08:04
  • This code is for version 3.x.x. Maybe they changed the behavior in the new versions (current is 4.2.x) and this code is no longer needed. Have you checked? – Martin from WP-Stars.com Feb 27 '19 at 09:04
  • 1
    Still they do not support x-default and need to add manually. Using your code is not replacing the default language but instead it is adding a new link. – edonbajrami Feb 27 '19 at 11:21
-1

I had the same problem and I didn't want to overwrite the core WPML files nor did I find a solution working with a WPML filter, so I just wrote the x-default hreflang to the WordPress Header like this:

 // add x-default to hreflang
function x_default_hreflang() {
    $languages = icl_get_languages('skip_missing=1');
    foreach($languages as $l){
        if ( $l['language_code'] == 'en' ) { // set your default language
            $x_default_url = $l['url'];
            $output='<link rel="alternate" hreflang="x-default" href="' . $x_default_url . '" />'  . PHP_EOL;
            echo $output;
        } 
    }
}

add_action('wp_head','x_default_hreflang',1);

The priority on the add_action is rather high, so the alternate hreflang link is outputed beneath the other ones, but as far as I know, that's not really necessary.