Skip to content

Instantly share code, notes, and snippets.

@khromov
Last active October 9, 2022 21:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khromov/b05d2b80a151f324f6b7dbad08db74ea to your computer and use it in GitHub Desktop.
Save khromov/b05d2b80a151f324f6b7dbad08db74ea to your computer and use it in GitHub Desktop.
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;
});
@halilnevzat
Copy link

its not accepting i dont know why. im getting php error in function.php file

@khromov
Copy link
Author

khromov commented Oct 9, 2022

@halilnevzat Without seeing the full error message, there is no way to tell.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment