Skip to content
Advertisement

Issues with hreflang php function on a multilingual site

I have been asked by one of my clients to troubleshoot why their WordPress website is having issues loading. Their site was build by a different developer and no longer does business with my client anymore so seeking help from them is an issue.

When I was investigating the cause I found the following PHP error:

[09-Jan-2017 04:09:52 UTC] PHP Fatal error: Can't use function return value in write context in /home/*********/public_html/fr/wp-content/themes/********/functions.php on line 121

When looking at the functions.php file I found the below code:

function bs_get_hreflang_tags() {
    ob_start();
    if( !empty( get_field('australia', get_the_ID() ) ) ) : ?>
        <link rel="alternate" href="<?php echo esc_url( get_field('australia', get_the_ID() ) ); ?>" hreflang="en-au" />
    <?php endif;
    if( !empty( get_field('france', get_the_ID() ) ) ) : ?>
        <link rel="alternate" href="<?php echo esc_url( get_field('france', get_the_ID() ) ); ?>" hreflang="fr"/>
    <?php endif;
    if( !empty( get_field('spain', get_the_ID() ) ) ) : ?>
        <link rel="alternate" href="<?php echo esc_url( get_field('spain', get_the_ID() ) ); ?>" hreflang="es" />
    <?php endif;
    if( !empty( get_field('italy', get_the_ID() ) ) ) : ?>
        <link rel="alternate" href="<?php echo esc_url( get_field('italy', get_the_ID() ) ); ?>" hreflang="it" />
    <?php endif;

    $output = ob_get_contents();
    ob_end_clean();

    return $output;
}

EDIT: line 121 is if( !empty( get_field('australia', get_the_ID() ) ) ) : ?>

I don’t fully understand what this code is doing however I believe it is mostly related to multilingual SEO support.

As I didn’t write the code or the original website, I was hoping to get some support on how to either patch the code so that it works or find alternative code to create the same job without causing issues.

I have commented out the code for the time being to allow the site to run. I was just hoping that someone might know the answer to this.

Any help would be appreciated

Advertisement

Answer

The ob construction is needlessly complicated.. Try replacing it with this.

function bs_get_hreflang_tags() {
    $output= '';
    if( get_field('australia', get_the_ID() ) ) :
        $output = '<link rel="alternate" href="'.esc_url( get_field('australia', get_the_ID() ) ) . '" hreflang="en-au" />';
    elseif( get_field('france', get_the_ID() ) ) :
        $output = '<link rel="alternate" href="'.esc_url( get_field('france', get_the_ID() ) ) . '" hreflang="fr" />';
    elseif( get_field('spain', get_the_ID() ) ) :
        $output = '<link rel="alternate" href="'.esc_url( get_field('spain', get_the_ID() ) ) . '" hreflang="es" />';
    elseif( get_field('italy', get_the_ID() ) ) :
        $output = '<link rel="alternate" href="'.esc_url( get_field('italy', get_the_ID() ) ) . '" hreflang="it" />';
    endif;

    return $output;
}
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement