> For the complete documentation index, see [llms.txt](https://developers.weglot.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developers.weglot.com/cms/wordpress/use-cases/add-translated-url-to-rankmath-sitemap-index.md).

# Add translated url to Rankmath sitemap index

First step, add this snippet to be sure to purge the sitemap caching

```php
/**
 * Filter if XML sitemap transient cache is enabled.
 *
 * @param boolean $unsigned Enable cache or not, defaults to true
 */
add_filter( 'rank_math/sitemap/enable_caching', '__return_false');
```

Add this filter. For the moment you need to add the list of url you want to translate into your sitemap

```php
add_filter( 'rank_math/sitemap/index', function( $xml ) {
	// Fetch Weglot services
	$language_services = weglot_get_service( 'Language_Service_Weglot' );
	$request_url_services = weglot_get_service( 'Request_Url_Service_Weglot' );

	// List of URLs to be translated
	$urls = [
		"http://beta-429.local/post-sitemap.xml",
		"http://beta-429.local/page-sitemap.xml",
		"http://beta-429.local/category-sitemap.xml",
	];

	$destination_languages = weglot_get_destination_languages();
	foreach ( $urls as $url ) {
		$wg_url = $request_url_services->create_url_object( $url );

		// Iterate over each destination language
		foreach ( $destination_languages as $language_code ) {
			$language = $language_services->get_language_from_internal( $language_code['language_to'] );

			$translated_url = $wg_url->getForLanguage( $language );

			$xml .= '
                <sitemap>
                    <loc>' . esc_url( $translated_url ) . '</loc>
                    <lastmod>' . esc_html( date( 'c' ) ) . '</lastmod>
                </sitemap>';
		}
	}

	return $xml;
}, 999 );
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://developers.weglot.com/cms/wordpress/use-cases/add-translated-url-to-rankmath-sitemap-index.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
