Fixing the dot problem in polylang

The hreflang attribute looks like this:

Such a record gives information to search engines that the multilingual page has an alternative in another language, these pages should be distinguished, indexed under different links and avoid duplicate content.
The polylang problem is noticed if there is a dot in the prefix fields for categories or for tags in the permalink settings:

And this is what hreflang looks like:

This entry is incorrect.
Let me remind you that a dot is placed in the prefix fields if it is necessary to remove the display of the prefix in the urls for categories or tags.
You can solve this problem by writing a callback function for pll_rel_hreflang_attributes. Insert the following code into functions.php of your theme:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
/** * Edit hreflangs in Polylang */ add_filter('pll_rel_hreflang_attributes', 'edit_polylang_hreflang_attributes', 10, 1); function edit_polylang_hreflang_attributes ($hreflangs) { if (is_category()) { foreach ( $hreflangs as $lang => $url ) { $hreflangs[$lang] = str_replace('/./', '/', $url); } } return $hreflangs; } |
As a result, we get these attributes:

Tested on polylang 3.2.5