WP_Sitemaps_Provider{}abstractWP 5.5.0AllowDynamicProperties

Basic abstract class for creating a Sitemap provider in WordPress.

The provider is created by creating your own class based on this and then registering it using the wp_register_sitemap_provider() function.

add_filter( 'init', 'wpkama_register_sitemap_providers' );
function wpkama_register_sitemap_providers(){

	require_once __DIR__ .'/class-My_Sitemaps_Provider.php';

	$provider = new Coredata_Sitemaps_Provider();

	wp_register_sitemap_provider( 'provider_name', $provider );
}
Hooks from the class

Usage

class My_Sitemaps_Provider extends WP_Sitemaps_Provider {

	public function __construct() {
		$this->name        = 'name';
		$this->object_type = 'type';
	}

	/**
	 * Gets a URL list for a sitemap.
	 *
	 * @param int    $page_num       Page of results.
	 * @param string $object_subtype Optional. Object subtype name. Default empty.
	 *
	 * @return array Array of URLs for a sitemap.
	 */
	public function get_url_list( $page_num, $object_subtype = '' ) {

		// code here

		return $url_list;
	}

	/**
	 * Gets the max number of pages available for the object type.
	 *
	 * @param string $object_subtype Optional. Object subtype. Default empty.
	 * @return int Total number of pages.
	 */
	public function get_max_num_pages( $object_subtype = '' ) {

		$total = 99999;

		return (int) ceil( $total / wp_sitemaps_get_max_urls( $this->object_type ) );
	}
}

Properties

$name(string) (required)

Name of the provider. Used in the sitemap URL. Must be unique. For example: /wp-sitemap-posts-post-1.xml - posts here is the provider name.

IMPORTANT! Only characters a-z are allowed. That is, dashes, spaces, and uppercase letters cannot be used. Incorrect: similar_posts, similar-posts, Similar. Correct similarposts.

$object_type(string) (required)
Name of the object the provider works with (post, term, user). Must be unique. Used/passed in hooks. For example, the second parameter of the hook wp_sitemaps_index_entry.

Methods

Methods that usually need to be defined in the inherited class:

get_object_subtypes()
Must return a list of subtypes, for example, for posts, these are post types. Returns an array of objects, with the keys containing the names of the subtypes.
get_url_list( $page_num, $subtype = '' )

Must return a list of links for each sitemap — a list of data for XML tags <url>. This list looks like an array of arrays, each nested array is each link. For example:

$url_list = [
	[ 'loc'=> 'https://example.com/megamedia/audio/2610735' ],
	[ 'loc'=> 'https://example.com/megamedia/audio/9514241' ],
	...
];
get_max_num_pages( $subtype = '' )
Must return an int - the total number of elements for the given subtype. For example, for WP posts - this is how many posts of the specified post type there are in total, for example, how many pages (page) there are in total.
db_query( $args )
This is a custom method of the class that creates a query in the database and retrieves data based on the passed parameters, including taking into account the subtype and pagination.

All methods:

  1. public get_url_list( $page_num, $object_subtype = '' )
  2. public get_max_num_pages( $object_subtype = '' )
  3. public get_sitemap_type_data()
  4. public get_sitemap_entries()
  5. public get_sitemap_url( $name, $page )
  6. public get_object_subtypes()

Examples

0

#1 Providers out of the box

0

#2 Example of creating a provider

see here.

Changelog

Since 5.5.0 Introduced.

WP_Sitemaps_Provider{} code WP 6.9.1

abstract class WP_Sitemaps_Provider {
	/**
	 * Provider name.
	 *
	 * This will also be used as the public-facing name in URLs.
	 *
	 * @since 5.5.0
	 *
	 * @var string
	 */
	protected $name = '';

	/**
	 * Object type name (e.g. 'post', 'term', 'user').
	 *
	 * @since 5.5.0
	 *
	 * @var string
	 */
	protected $object_type = '';

	/**
	 * Gets a URL list for a sitemap.
	 *
	 * @since 5.5.0
	 *
	 * @param int    $page_num       Page of results.
	 * @param string $object_subtype Optional. Object subtype name. Default empty.
	 * @return array[] Array of URL information for a sitemap.
	 */
	abstract public function get_url_list( $page_num, $object_subtype = '' );

	/**
	 * Gets the max number of pages available for the object type.
	 *
	 * @since 5.5.0
	 *
	 * @param string $object_subtype Optional. Object subtype. Default empty.
	 * @return int Total number of pages.
	 */
	abstract public function get_max_num_pages( $object_subtype = '' );

	/**
	 * Gets data about each sitemap type.
	 *
	 * @since 5.5.0
	 *
	 * @return array[] Array of sitemap types including object subtype name and number of pages.
	 */
	public function get_sitemap_type_data() {
		$sitemap_data = array();

		$object_subtypes = $this->get_object_subtypes();

		/*
		 * If there are no object subtypes, include a single sitemap for the
		 * entire object type.
		 */
		if ( empty( $object_subtypes ) ) {
			$sitemap_data[] = array(
				'name'  => '',
				'pages' => $this->get_max_num_pages(),
			);
			return $sitemap_data;
		}

		// Otherwise, include individual sitemaps for every object subtype.
		foreach ( $object_subtypes as $object_subtype_name => $data ) {
			$object_subtype_name = (string) $object_subtype_name;

			$sitemap_data[] = array(
				'name'  => $object_subtype_name,
				'pages' => $this->get_max_num_pages( $object_subtype_name ),
			);
		}

		return $sitemap_data;
	}

	/**
	 * Lists sitemap pages exposed by this provider.
	 *
	 * The returned data is used to populate the sitemap entries of the index.
	 *
	 * @since 5.5.0
	 *
	 * @return array[] Array of sitemap entries.
	 */
	public function get_sitemap_entries() {
		$sitemaps = array();

		$sitemap_types = $this->get_sitemap_type_data();

		foreach ( $sitemap_types as $type ) {
			for ( $page = 1; $page <= $type['pages']; $page++ ) {
				$sitemap_entry = array(
					'loc' => $this->get_sitemap_url( $type['name'], $page ),
				);

				/**
				 * Filters the sitemap entry for the sitemap index.
				 *
				 * @since 5.5.0
				 *
				 * @param array  $sitemap_entry  Sitemap entry for the post.
				 * @param string $object_type    Object empty name.
				 * @param string $object_subtype Object subtype name.
				 *                               Empty string if the object type does not support subtypes.
				 * @param int    $page           Page number of results.
				 */
				$sitemap_entry = apply_filters( 'wp_sitemaps_index_entry', $sitemap_entry, $this->object_type, $type['name'], $page );

				$sitemaps[] = $sitemap_entry;
			}
		}

		return $sitemaps;
	}

	/**
	 * Gets the URL of a sitemap entry.
	 *
	 * @since 5.5.0
	 *
	 * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
	 *
	 * @param string $name The name of the sitemap.
	 * @param int    $page The page of the sitemap.
	 * @return string The composed URL for a sitemap entry.
	 */
	public function get_sitemap_url( $name, $page ) {
		global $wp_rewrite;

		// Accounts for cases where name is not included, ex: sitemaps-users-1.xml.
		$params = array_filter(
			array(
				'sitemap'         => $this->name,
				'sitemap-subtype' => $name,
				'paged'           => $page,
			)
		);

		$basename = sprintf(
			'/wp-sitemap-%1$s.xml',
			implode( '-', $params )
		);

		if ( ! $wp_rewrite->using_permalinks() ) {
			$basename = '/?' . http_build_query( $params, '', '&' );
		}

		return home_url( $basename );
	}

	/**
	 * Returns the list of supported object subtypes exposed by the provider.
	 *
	 * @since 5.5.0
	 *
	 * @return array List of object subtypes objects keyed by their name.
	 */
	public function get_object_subtypes() {
		return array();
	}
}