WP_Sitemaps_Renderer::get_sitemap_xml() WordPress Method

The WP_Sitemaps_Renderer::get_sitemap_xml() method is used to generate an XML sitemap for a given website. This sitemap can be used to improve the website's search engine optimization (SEO) by helping search engines index the website's content more effectively. The XML sitemap generated by this method will include all of the website's public posts and pages, as well as any other public content that is registered with WordPress.

WP_Sitemaps_Renderer::get_sitemap_xml( array $url_list ) #

Gets XML for a sitemap.


Parameters

$url_list

(array)(Required)Array of URLs for a sitemap.


Top ↑

Return

(string|false) A well-formed XML string for a sitemap index. False on error.


Top ↑

Source

File: wp-includes/sitemaps/class-wp-sitemaps-renderer.php

	public function get_sitemap_xml( $url_list ) {
		$urlset = new SimpleXMLElement(
			sprintf(
				'%1$s%2$s%3$s',
				'<?xml version="1.0" encoding="UTF-8" ?>',
				$this->stylesheet,
				'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" />'
			)
		);

		foreach ( $url_list as $url_item ) {
			$url = $urlset->addChild( 'url' );

			// Add each element as a child node to the <url> entry.
			foreach ( $url_item as $name => $value ) {
				if ( 'loc' === $name ) {
					$url->addChild( $name, esc_url( $value ) );
				} elseif ( in_array( $name, array( 'lastmod', 'changefreq', 'priority' ), true ) ) {
					$url->addChild( $name, esc_xml( $value ) );
				} else {
					_doing_it_wrong(
						__METHOD__,
						sprintf(
							/* translators: %s: List of element names. */
							__( 'Fields other than %s are not currently supported for sitemaps.' ),
							implode( ',', array( 'loc', 'lastmod', 'changefreq', 'priority' ) )
						),
						'5.5.0'
					);
				}
			}
		}

		return $urlset->asXML();
	}


Top ↑

Changelog

Changelog
VersionDescription
5.5.0Introduced.

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.