get_replace_modify_link_in_xml
This filter allow you to add pattern to define kind of link you want to translate in xml.
here the list of pattern we already treat on Weglot:
array('loc' => '/<loc>(.*?)<\/loc>/');The filter receives this array and must return it. Each key you add needs a global function named replace_<key>( $translated_page, $current_url ): Weglot calls it for every URL the pattern captures (its first capture group) and uses the page it returns. Without that function the pattern is ignored. Rename the function with the weglot_modify_link_sitemap_replace_function filter.
add_filter( 'get_replace_modify_link_in_xml', 'custom_weglot_xml_link_patterns' );
function custom_weglot_xml_link_patterns( $patterns ) {
$patterns['page_loc'] = '/<page:loc>([^<]+)<\/page:loc>/';
return $patterns;
}
function replace_page_loc( $translated_page, $current_url ) {
$replace_link_service = weglot_get_service( 'Replace_Link_Service_Weglot' );
$current_language = weglot_get_service( 'Request_Url_Service_Weglot' )->get_current_language();
return str_replace(
'<page:loc>' . $current_url . '</page:loc>',
'<page:loc>' . esc_url( $replace_link_service->replace_url( $current_url, $current_language ) ) . '</page:loc>',
$translated_page
);
}Only target tags holding page URLs. File URLs (images, PDF…) are skipped, and the <xhtml:link rel="alternate"> tags of a sitemap are its hreflang cluster: rewriting them to the current language would break it.
Last updated