Hreflang Tags with Errors

Hello,

I received an error from Google search console about wrong hreflang implementation error on my subfolder htttps://tarekrabaa.com/en/

International Targeting | Language > ‘ar’ - no return tags

any idea how to fix it?

thx

Hi there,

Thanks for writing in.

It’s this code <link rel="alternate" hreflang="ar" href="https://tarekrabaa.com/" />.

How did you add it? It should be something like

<link rel="alternate" hreflang="en-us" href="http://www.example.com/usa/" />
<link rel="alternate" hreflang="en-ca" href="http://www.example.com/ca/" />

Depending on the language and URL it represent. I recommend checking your codes related to this, if it’s caused by a plugin then you may reach the plugin author as well.

Thanks!

I added this code in the base.php
`<?php

// =============================================================================
// VIEWS/HEADER/BASE.PHP
// -----------------------------------------------------------------------------
// Declares the DOCTYPE for the site, includes the , opens the
// element as well as the .x-root

and .x-site
.
// =============================================================================

$x_root_atts = x_atts( apply_filters( ‘x_root_atts’, array( ‘id’ => ‘x-root’, ‘class’ => ‘x-root’ ) ) );
$x_site_atts = x_atts( apply_filters( ‘x_site_atts’, array( ‘id’ => ‘x-site’, ‘class’ => ‘x-site site’ ) ) );

$blog_id = get_current_blog_id();

if ( $blog_id == 13 ) {
$lang_tag = ‘lang=“en-US”’;
$lang_alter = ‘’;
}
if ( $blog_id == 1 ) {
$lang_tag = ‘lang=“ar”’;
$lang_alter = ‘’;
}
?>

> <?php wp_head(); ?> <?php echo $lang_alter; ?> >
>
<?php do_action( 'x_before_site_begin' ); ?>

<div <?php echo $x_site_atts; ?>>

<?php do_action( 'x_after_site_begin' ); ?>`

Hello Tarek,

I remember I provided you with this snippet here, the code is working fine, but you need to read more about these languages annotations, I searched Google and I found this information here:

If you have multiple language versions of a URL, each language page should identify different language versions, including itself. For example, if your site provides content in French, English, and Spanish, the Spanish version must include a rel=“alternate” hreflang=“x” link for itself in addition to links to the French and English versions. Similarly, the English and French versions must each include the same references to the French, English, and Spanish versions.

So, please update the code in “/pro-child/framework/views/header/base.php” to:

<?php

// =============================================================================
// VIEWS/HEADER/BASE.PHP
// -----------------------------------------------------------------------------
// Declares the DOCTYPE for the site, includes the <head>, opens the <body>
// element as well as the .x-root <div> and .x-site <div>.
// =============================================================================

$x_root_atts = x_atts( apply_filters( 'x_root_atts', array( 'id' => 'x-root', 'class' => 'x-root' ) ) );
$x_site_atts = x_atts( apply_filters( 'x_site_atts', array( 'id' => 'x-site', 'class' => 'x-site site' ) ) );

$blog_id = get_current_blog_id();

if ( $blog_id == 1 ) {
    $lang_tag = 'lang="en-US"';
    $lang_alter = '<link rel="alternate" hreflang="en" href="https://tarekrabaa.com/en" />
                  <link rel="alternate" hreflang="ar" href="https://tarekrabaa.com/" />';  
}
if ( $blog_id == 2 ) {
    $lang_tag = 'lang="ar"';
    $lang_alter = '<link rel="alternate" hreflang="ar" href="https://tarekrabaa.com/" />
                  <link rel="alternate" hreflang="en" href="https://tarekrabaa.com/en" />';  
}
?>

<!DOCTYPE html>

<html class="no-js" <?php echo $lang_tag ?>>

<head>
  <?php wp_head(); ?>
  <?php echo $lang_alter; ?>
</head>

<body <?php body_class(); ?>>

  <div <?php echo $x_root_atts; ?>>

    <?php do_action( 'x_before_site_begin' ); ?>

    <div <?php echo $x_site_atts; ?>>

    <?php do_action( 'x_after_site_begin' ); ?>

As this is all custom development, regretfully we wont be able to assist further. Custom development is outside the scope of our support. We are happy to provide advice and get you started in the right direction, but you would still be responsible for the implementation. And moreover, this solution may not work after several updates especially if the themes structure changed or overridden.

Thanks.

1 Like

thx for rewriting the new code.

You’re most welcome!

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.