get_sitemap_url() WordPress Function

The get_sitemap_url() function in WordPress allows you to easily get the URL for the current site's sitemap. This is useful if you need to programmatically generate a sitemap for your site.

get_sitemap_url( string $name, string $subtype_name = '', int $page = 1 ) #

Retrieves the full URL for a sitemap.


Parameters

$name

(string)(Required)The sitemap name.

$subtype_name

(string)(Optional)The sitemap subtype name.

Default value: ''

$page

(int)(Optional)The page of the sitemap.

Default value: 1


Top ↑

Return

(string|false) The sitemap URL or false if the sitemap doesn't exist.


Top ↑

Source

File: wp-includes/sitemaps.php

function get_sitemap_url( $name, $subtype_name = '', $page = 1 ) {
	$sitemaps = wp_sitemaps_get_server();

	if ( ! $sitemaps ) {
		return false;
	}

	if ( 'index' === $name ) {
		return $sitemaps->index->get_index_url();
	}

	$provider = $sitemaps->registry->get_provider( $name );
	if ( ! $provider ) {
		return false;
	}

	if ( $subtype_name && ! in_array( $subtype_name, array_keys( $provider->get_object_subtypes() ), true ) ) {
		return false;
	}

	$page = absint( $page );
	if ( 0 >= $page ) {
		$page = 1;
	}

	return $provider->get_sitemap_url( $subtype_name, $page );
}


Top ↑

Changelog

Changelog
VersionDescription
5.5.1Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.