Skip to content

Instantly share code, notes, and snippets.

@rmpel
Last active February 12, 2020 07:24
Show Gist options
  • Save rmpel/b00997f2504c39c718028dfa7e79b87c to your computer and use it in GitHub Desktop.
Save rmpel/b00997f2504c39c718028dfa7e79b87c to your computer and use it in GitHub Desktop.
Fix the output of the XML sitemap so they show the correct localised URLs. This is an add-on for when you change the URLs using other techniques, see README.
<?php
// supports Yoast SEO and Yoast SEO Premium
add_filter('wpseo_sitemap_entry', 'wpseo_sitemap_entry', 10, 3);
// Supports SEO by Rank Math
add_filter('rank_math/sitemap/entry', 'wpseo_sitemap_entry', 10, 3);
function wpseo_sitemap_entry ( $url, $type, $object ) {
global $sitepress;
$current_language = $sitepress->get_current_language();
if ($type == 'term') {
$data = null;
$search = array('element_id' => $object->term_id, 'element_type' => $object->taxonomy );
$language = apply_filters('wpml_element_language_details', $data, $search);
if ($language && is_object($language) && isset($language->language_code)) {
$sitepress->switch_lang($language->language_code);
$url['loc'] = get_term_link($object, $object->taxonomy);
}
$sitepress->switch_lang($current_language);
}
else if ($type != 'user') { // post, page, cpts
$data = null;
$search = array('element_id' => $object->ID, 'element_type' => $type );
$language = apply_filters('wpml_element_language_details', $data, $search);
if ($language && is_object($language) && isset($language->language_code)) {
$sitepress->switch_lang($language->language_code);
$url['loc'] = get_permalink($object->ID);
}
$sitepress->switch_lang($current_language);
}
return $url;
}

Fix the output of the XML sitemap so they show the correct localised URLs. This is an add-on for when you change the URLs using other techniques.

With WPML you can localize the slug of your CPT single, but not your CPT archive, if you give it a different content. You can fix the translations with https://gist.github.com/rmpel/d9989da79956fb0eafe81e49331b84b6 .

With WPML you can localize the "page for posts", but this will not localize the post-URL-base. You can fix this with https://gist.github.com/rmpel/5672b2b943a4a015fe1777c0a31fac59 .

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