For the complete documentation index, see llms.txt. This page is also available as Markdown.

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
    );
}

Last updated