How to add hreflang tags (or other meta tags) to pages in magento?
You have to loop through your stores and get URL and locale for each:
foreach (Mage::app()->getWebsites() as $website) {
foreach ($website->getGroups() as $group) {
$stores = $group->getStores();
foreach ($stores as $store) {
echo '<link rel="alternate" href="' . $store->getCurrentUrl() . '" hreflang="' . $store->getConfig('general/locale/code') . '"/>' . "\n";
}
}
}
In my case, I wanted to do different hreflangs for each website. So to do just for the current one:
$website = Mage::app()->getWebsite()->getStores();
foreach ( $website as $store) {
$lang = $store->getConfig('general/locale/code');
echo '<link rel="alternate" href="' . $store->getCurrentUrl() . '" hreflang="' . $lang . '"/>' . "\n";
}
There is one more thing to point out, $lang = $store->getConfig('general/locale/code');
generates language tags like so: "en_GB".
As Google states, that is incorrect, it should have been: "en-gb" (dash not underscore, small caps) or "en", depended on what you wan to achieve - link.
I use the code directly in templates, as it each of my website has its own template - it should go to app/design/frontend/yourPackage/yourTemplate/template/page/html/head.phtml
An example of replacing language tags as asked in comments - in most cases we only use "non targeted" language tags (two leter ISO codes):
$lang = $store->getConfig('general/locale/code');
if (strtolower($lang) == 'en_us'){
$lang = 'en'; //OR en-gb or any tag you need
}
echo '<link rel="alternate" href="' . $store->getCurrentUrl() . '" hreflang="' . $lang . '"/>' . "\n";