Last active
October 9, 2022 21:15
-
-
Save khromov/b05d2b80a151f324f6b7dbad08db74ea to your computer and use it in GitHub Desktop.
Multilingual Polylang sitemaps for "XML Sitemap & Google News" plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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
@halilnevzat Without seeing the full error message, there is no way to tell.