To set up a WPML multilingual sitemap in WordPress, you can use the wpml_add_sitemap_subdomain
filter hook provided by the WPML plugin. This code snippet demonstrates how to add the language subdomain to the sitemap URLs generated by WPML:
function wpsnippets_add_language_subdomain_to_sitemap_urls( $url ) {
$current_language = apply_filters( 'wpml_current_language', NULL );
if ( $current_language ) {
$url = str_replace( home_url(), home_url( $current_language ), $url );
}
return $url;
}
add_filter( 'wpml_add_sitemap_subdomain', 'wpsnippets_add_language_subdomain_to_sitemap_urls' );
This code snippet adds the language subdomain to the sitemap URLs generated by WPML. It uses the wpml_add_sitemap_subdomain
filter hook to modify the sitemap URL before it is outputted. The wpsnippets_add_language_subdomain_to_sitemap_urls
function retrieves the current language using the wpml_current_language
filter hook and replaces the base URL with the language-specific URL using str_replace()
. Finally, the modified URL is returned.
This code snippet is useful when you want to include language subdomains in your WPML multilingual sitemap URLs. It ensures that search engines can properly index and crawl the different language versions of your website.
Examples
Example 1: Creating a Multilingual Sitemap with WPML
This example demonstrates how to create a multilingual sitemap using WPML, a popular WordPress plugin for managing multilingual websites. The code example below shows how to add a custom endpoint to the WordPress REST API to generate the sitemap.
function wpsnippets_create_multilingual_sitemap() {
register_rest_route( 'wpsnippets/v1', '/sitemap', array(
'methods' => 'GET',
'callback' => 'wpsnippets_generate_sitemap',
) );
}
function wpsnippets_generate_sitemap() {
// Generate the sitemap XML here
// Include URLs for all languages supported by WPML
// Return the sitemap XML as a string
}
add_action( 'rest_api_init', 'wpsnippets_create_multilingual_sitemap' );
In this code example, we first register a custom REST API route /sitemap
using the register_rest_route()
function. We specify the HTTP method as GET
and the callback function as wpsnippets_generate_sitemap
. Inside the wpsnippets_generate_sitemap
function, you would generate the sitemap XML, including URLs for all languages supported by WPML, and return it as a string.
Example 2: Adding Language-Specific URLs to the Sitemap
This example demonstrates how to add language-specific URLs to the sitemap generated in the previous example. We’ll use the icl_get_languages()
function provided by WPML to retrieve the list of languages and their corresponding URLs.
function wpsnippets_generate_sitemap() {
$languages = icl_get_languages();
// Generate the sitemap XML here
foreach ( $languages as $language ) {
$language_url = $language['url'];
// Add language-specific URLs to the sitemap
}
// Return the sitemap XML as a string
}
In this code example, we retrieve the list of languages and their URLs using the icl_get_languages()
function. We then loop through each language and add the language-specific URLs to the sitemap XML. You would implement the logic to generate the sitemap XML and add the URLs accordingly.
Example 3: Excluding Certain Pages from the Sitemap
This example demonstrates how to exclude certain pages from the sitemap generated by WPML. We’ll use the wpseo_sitemap_exclude_post_type
filter provided by the Yoast SEO plugin to exclude specific post types from the sitemap.
function wpsnippets_exclude_pages_from_sitemap( $value, $post_type ) {
if ( $post_type === 'page' ) {
$excluded_pages = array( 1, 2, 3 ); // IDs of pages to exclude
$page_id = get_the_ID();
if ( in_array( $page_id, $excluded_pages ) ) {
return true; // Exclude the page from the sitemap
}
}
return $value;
}
add_filter( 'wpseo_sitemap_exclude_post_type', 'wpsnippets_exclude_pages_from_sitemap', 10, 2 );
In this code example, we define an array of page IDs that we want to exclude from the sitemap. Inside the wpsnippets_exclude_pages_from_sitemap
function, we check if the current post type is a page and if its ID is in the array of excluded pages. If it is, we return true
to exclude the page from the sitemap. You can modify the array of excluded page IDs to suit your needs.