• Resolved tacoberry

    (@tacoberry)


    I am currently running a WP multisite with two different versions that I want to show different “language-country” hreflangs for in my header. I managed to add the following code to my functions.php file in order to make the country code appear in the hreflang as well:

    Code added to functions.php

    /**
     * @param string $language
     * @return string
     */
    function my_msls_head_hreflang( $language ) {
    	if ( 'en' == $language ) {
    		$language = 'en-us';
    	}
    	return $language;
    }
    add_filter( 'msls_head_hreflang', 'my_msls_head_hreflang' );

    Now the hreflang tags shown in my header use underscores (_) instead of hyphens (-). However, they need to be hyphens as this is the only format search engines will recognize.

    Now my hreflangs look like this:

    <link rel="alternate" hreflang="en_US" href="http://example.com/" />
    <link rel="alternate" hreflang="en_GB" href="http://example.com/gb/" />

    While they should look like this:

    <link rel="alternate" hreflang="en-US" href="http://example.com/" />
    <link rel="alternate" hreflang="en-GB" href="http://example.com/gb/" />

    I have my WPLANG in my multisite settings configured with underscores: “en_US” for instance. Changing this however doesn’t seem to make a difference.

    Any thoughts?

    https://wordpress.org/plugins/multisite-language-switcher/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Dennis Ploetner

    (@realloc)

    Hi,

    I’m pretty sure that $language is not set to ‘en’. Please check first what you have here. I think it will be something like ‘en_US’ or ‘en_GB’ in your case.

    To change just the underscores to hyphens here I would do something like this:

    /**
     * @param string $language
     * @return string
     */
    function my_msls_head_hreflang( $language ) {
    	return str_replace( '_', '-', $language );
    }
    add_filter( 'msls_head_hreflang', 'my_msls_head_hreflang' );

    Cheers,
    Dennis.

    Thread Starter tacoberry

    (@tacoberry)

    Hi Dennis,

    Big thanks for replying so quickly! Replacing my code with the one you provided did the trick for me. The hreflangs are now showing “language-country” with the hyphen. I’m glad it’s working, thanks! 🙂

    Plugin Author Dennis Ploetner

    (@realloc)

    OK, very good!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Replacing underscores by hyphens in hreflang’ is closed to new replies.