Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Multilingual Polylang sitemaps for "XML Sitemap & Google News" plugin
<?php
/**
* Plugin Name: Multilingual Polylang sitemaps for "XML Sitemap & Google News" plugin
* Description: For https://wordpress.org/plugins/xml-sitemap-feed/
* Version: 1.0
*/
add_action('xmlsf_tags_after', function () { // xmlsf_tags_after
//Bail early.
if (!function_exists('pll_languages_list')) {
return null;
}
$languages = pll_languages_list(['fields' => '',]); // Returns all fields
/* @var $language PLL_Language */
foreach ($languages as $language) {
$id = get_the_ID();
$translated_post = pll_get_post($id, $language->slug);
$hreflang = $language->slug;
//Special case for EN
if (in_array($language->slug, ['eng'])) {
$hreflang = $language->w3c;
}
if ($translated_post) {
?>
<xhtml:link rel="alternate" hreflang="<?php echo $hreflang; ?>" href="<?php echo get_the_permalink(pll_get_post($id, $language->slug)); ?>" />
<?php
}
}
});
add_action('xmlsf_urlset', function ($type) {
if ($type === 'post_type') {
echo 'xmlns:xhtml="http://www.w3.org/1999/xhtml"' . PHP_EOL;
}
});
//Make en-GB one the one to use for all of en locale
add_filter('pll_rel_hreflang_attributes', function ($hreflangs) {
if (isset($hreflangs['en-GB'])) {
$hreflangs['en'] = $hreflangs['en-GB'];
unset($hreflangs['en-GB']);
}
return $hreflangs;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.