How can I overwrite the function code of a WP plugin (WPML)?

I need help please overwritting a WPML function which outputs the rel-alternate-hreflang tag. Ideally you would provide me a function that I can place in my child themes function.php file.

I am using the popular WPML (WordPress Multi Language) plugin to manage multiple languages on a WP Multi site. I have one site (Canadian) that is English and French, and other site (USA) that is English and Spanish. I am wanting to tell Google about the other site with respect to implementing the rel-alternate-hreflang tag. This should help Google direct Canadians to the Canadian equivalent page and vice-versa for US visitors. Currently the WPML plugin (which manages BOTH sites in the multisite) outputs …

Read More

On the USA web site ….

<link rel="alternate" hreflang="en-US" href="http://siteaddress.com/pagename" />
<link rel="alternate" hreflang="es" href="http://siteaddress.com/es/spanishpagename" />

On the Canadian web site ….

<link rel="alternate" hreflang="en-CA" href="http://siteaddress.com/ca/pagename" />
<link rel="alternate" hreflang="fr" href="http://siteaddress.com/es/spanishpagename" />

BUT I WANT IT TO OUTPUT …
(note the English page names are identical, the only difference is the /ca/. I can not do anything about the USA site knowing the name of the French page or the Canadian site knowing the Spanish page name. That information would not be available in each multisite database. However the CAN vs USA pagename urls are a simple calculation once the href link has been extracted)

On the USA web site ….

<link rel="alternate" hreflang="en-US" href="http://siteaddress.com/pagename" />
<link rel="alternate" hreflang="es" href="http://siteaddress.com/es/spanishpagename" />
<link rel="alternate" hreflang="en-CA" href="http://siteaddress.com/ca/pagename" />

On the Canadian web site ….

<link rel="alternate" hreflang="en-CA" href="http://siteaddress.com/ca/pagename" />
<link rel="alternate" hreflang="fr" href="http://siteaddress.com/es/spanishpagename" />
<link rel="alternate" hreflang="en-US" href="http://siteaddress.com/pagename" />

I tried asking for tech support from WPML but did not get the help I was hoping for. So I found the function that creates this output in the file “wp-content/plugins/sitepress-multilingual-cms/sitepress.class.php”. I don’t want to hack this file as the plugin gets updated regularly. So hopefully we can override it, if possible. Note this same script outputs BOTH the USA and Canadian output so we also need to figure out first which output it is creating before we can calculate the CAN vs USA equivalent page.

The function code looks like this …

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 ) );
        }
    }
}

Can this be done? Alternatively I was wondering if there was a way to find these tags with jQuery, get the page link, add “/ca” or remove it and insert the extra tag with the corresponding hreflang. But this is a question for a different post I suspect. My thought is the above solution I am looking for would be a better way to go.

Thanks for your assistance!

Related posts

1 comment

  1. You can remove execution of this function:

    remove_action('wp_head', array("SitePress","head_langs"))
    

    and then register your own function

    add_action("wp_head","my_own_head_langs_fn");
    function my_own_head_langs_fn() {
    // paste here your modified version of head_langs
    }
    

Comments are closed.