Skip to content

Instantly share code, notes, and snippets.

@wpflames
Created March 8, 2022 08:28
Show Gist options
  • Select an option

  • Save wpflames/8d950cb1f6e07487b9028114393fe468 to your computer and use it in GitHub Desktop.

Select an option

Save wpflames/8d950cb1f6e07487b9028114393fe468 to your computer and use it in GitHub Desktop.
Add hreflang attribute to head section
<?php
// =============================================================
// ADD HREFLANG ATTRIBUTE TO HEAD SECTION
// =============================================================
function add_hreflang_attr_to_head(){
$blog_id = get_current_blog_id();
// ENGLISH
// =======================
if($blog_id === 1){
echo '<link rel="alternate" hreflang="en" href="https://YOURWEBSITE.com" />';
echo '<link rel="alternate" hreflang="x-default" href="https://YOURWEBSITE.com" />';
}
// HUN
// =======================
else if($blog_id === 2){
echo '<link rel="alternate" hreflang="hu" href="https://YOURWEBSITE.com/hu/" />';
}
// ITALIAN
// =======================
else if($blog_id === 3){
echo '<link rel="alternate" hreflang="it" href="https://YOURWEBSITE.com/it/" />';
}
}
add_action('wp_head', 'add_hreflang_attr_to_head', 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment