Dear all,
I’m using C5 version 8.5.5 with PHP 7.3.33 and have implemented hreflang tag via /application/config/site.php.
I want all the alias pages to be added as an alternate URL.
I’m using the following code:
<?php
return [
'sites' => [
'default' => [
'multilingual' => [
'set_alternate_hreflang' => true,
],
],
],
];
Can anyone help me to add alias pages as alternate urls.
Thanks in advance.
From what I can see, the only time the core uses set_alternate_hreflang
is when setting alternate URLs in the header.
The core first triggers an event on_header_required_ready
which will allow you to modify the header’s content before it is used.
If you hook into that event, you can grab an array of all <link>
elements using
$event->getArgument('alternateHreflangTags');
That array was built like this:
$alternateHreflangTags[] = '<link rel="alternate" hreflang="'.str_replace('_', '-', $ms->getLocale()).'" href="'.$url.'">';
So all you have to do is loop through the array and replace each hreflang
attribute with the value you want.
Once you’re done, modify the argument with your modified array doing:
$event->setArgument('alternateHreflangTags', $modifiedAlternateHreflangTags);
Then return the $event object and the header will use your modified <link>
elements.
Here’s the documentation for hooking into an event:
I hope this helps