Function to create hreflang tags


The site is a test env. The site is in ES-MX but we have en, en-gb and es versions on a different domain.

The site has quite a few pages and rather than add the hreflang to each of them, I’d like to use a function to do the work. I found this example:

” hreflang=”en-au” />

This looks promising however I have the following questions – can I add it to functions.php? if so, do I need to add any code to header.php also? Will this sample code (thanks Chris Butterworth) need to be modified (aside from the URL and hreflang obviously :wink:

This is an example of the href tags that would need to go on each page and post of the site.

I hope I’ve explained clearly, though I fear I haven’t :wink:

Thanks for any assist you could provide!

  • Ash
    • WordPress Hacker

    Hello David

    ” hreflang=”en-au” /> – this code can’t be used in functions.php. Do you have any full sample code?

    You can also try to add the following line in header.php inside head tag:

    <base hreflang=”en-au” />

    Base tag works for anchor tag and you can define common attributes in base tag. If you add any attribute in base tag, then you don’t need to add those attribute in the anchor tag.

    Let us know if that works for you.

    Have a nice day!



Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.